IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.1.0
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

Bencher

struct Bencher

Defines a Bencher struct which facilitates the timing of a target function.

Fields​

  • ​num_iters (Int): Number of iterations to run the target function.
  • ​elapsed (Int): The total time elapsed when running the target function.

Implemented traits​

AnyType, Deinitable, Movable, RegisterPassable

Methods​

__init__​

def __init__(num_iters: Int) -> Self

Constructs a Bencher object to run and time a function.

Args:

  • ​num_iters (Int): Number of times to run the target function.

iter​

def iter[IterFn: def() -> None](mut self, f: IterFn)

Returns the total elapsed time by running a target closure a particular number of times.

Parameters:

  • ​IterFn (def() -> None): Type of the closure to benchmark.

Args:

  • ​f (IterFn): The closure to benchmark.

def iter[IterFn: def() raises -> None](mut self, f: IterFn)

Returns the total elapsed time by running a raising target closure a particular number of times.

Parameters:

  • ​IterFn (def() raises -> None): Type of the closure to benchmark.

Args:

  • ​f (IterFn): The closure to benchmark.

Raises:

If the closure raises.

iter_preproc​

def iter_preproc[T: AnyType, //, IterFn: def(mut T) -> None, PreprocFn: def(mut T) -> None](mut self, mut state: T, iter_fn: IterFn, preproc_fn: PreprocFn) where identical(IterFn.T, T) where identical(PreprocFn.T, T)

Accumulates the total elapsed time of a target function over the configured number of iterations, running a preprocess function to prepare the state before each timed call.

Both functions receive state mutably; only the target function's run time is measured.

Parameters:

  • ​T (AnyType): The type of the state passed to both functions.
  • ​IterFn (def(mut T) -> None): The target function type.
  • ​PreprocFn (def(mut T) -> None): The preprocess function type.

Args:

  • ​state (T): The state prepared by the preprocess function and consumed by the target function each iteration.
  • ​iter_fn (IterFn): The target function to benchmark.
  • ​preproc_fn (PreprocFn): The function preparing the state before each timed call.

iter_custom​

def iter_custom[FuncType: def(Int) -> Int](mut self, func: FuncType)

Times a target function with custom number of iterations.

Parameters:

  • ​FuncType (def(Int) -> Int): The target function type.

Args:

  • ​func (FuncType): The closure carrying the captured state of the target function.