Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

bench.fz


# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation.  If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
#  Tokiwa Software GmbH, Germany
#
#  Source code of Fuzion standard library feature bench
#
#  Author: Michael Lill (michael.lill@tokiwa.software)
#
# -----------------------------------------------------------------------

# benchmark f by iterating it for nano_seconds
#
# only completed iterations are counted, any iterations started before
# warm_up_nano_seconds has elapsed are not counted either
#
# returns: iterations per second
#
public bench(f ()->unit, nano_seconds u64, warm_up_nano_seconds u64) f64 ! time.nano =>
  start := time.nano.read

  is_warmup => time.nano.read - start < warm_up_nano_seconds

  for iter := (u64 0), if (is_warmup) iter else iter + 1
  while time.nano.read - start < warm_up_nano_seconds + nano_seconds
  do
    f.call
  else
    iter.as_f64 / (nano_seconds.as_f64 * 1E-9)


# benchmark f for milli_seconds (1s warm up)
#
# only completed iterations are counted, any iterations started before
# the one second warmup has elapsed are not counted either
#
# returns: iterations per second
#
public bench(f ()->unit, milli_seconds i32) f64 =>
  bench f (milli_seconds.as_u64 * 1E6) (u64 1E9)