Skip to main content
Log in

Mojo struct

Bencher

@register_passable 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 elpased when running the target function.

Implemented traits

AnyType, Copyable, Movable, UnknownDestructibility

Methods

__init__

@implicit __init__(out self, num_iters: Int)

Constructs a Bencher object to run and time a function.

Args:

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

iter

iter[: origin.set, //, iter_fn: fn() capturing -> None](mut self)

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

Parameters:

  • iter_fn (fn() capturing -> None): The target function to benchmark.

iter[iter_fn: fn() raises capturing -> None](mut self)

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

Parameters:

  • iter_fn (fn() raises capturing -> None): The target function to benchmark.

iter_preproc

iter_preproc[: origin.set, : origin.set, //, iter_fn: fn() capturing -> None, preproc_fn: fn() capturing -> None](mut self)

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

Parameters:

  • iter_fn (fn() capturing -> None): The target function to benchmark.
  • preproc_fn (fn() capturing -> None): The function to preprocess the target function.

iter_custom

iter_custom[: origin.set, //, iter_fn: fn(Int) capturing -> Int](mut self)

Times a target function with custom number of iterations.

Parameters:

  • iter_fn (fn(Int) capturing -> Int): The target function to benchmark.

iter_custom[: origin.set, //, kernel_launch_fn: fn(DeviceContextV2) raises capturing -> None](mut self, ctx: DeviceContextV2)

Times a target GPU function with custom number of iterations via DeviceContext ctx.

Parameters:

  • kernel_launch_fn (fn(DeviceContextV2) raises capturing -> None): The target GPU kernel launch function to benchmark.

Args:

  • ctx (DeviceContextV2): The GPU DeviceContext for launching kernel.

iter_custom[: origin.set, //, kernel_launch_fn: fn(DeviceContextV2, Int) raises capturing -> None](mut self, ctx: DeviceContextV2)

Times a target GPU function with custom number of iterations via DeviceContext ctx.

Parameters:

  • kernel_launch_fn (fn(DeviceContextV2, Int) raises capturing -> None): The target GPU kernel launch function to benchmark.

Args:

  • ctx (DeviceContextV2): The GPU DeviceContext for launching kernel.

iter_custom[iter_fn: fn(Int) raises capturing -> Int](mut self)

Times a target function with custom number of iterations.

Parameters:

  • iter_fn (fn(Int) raises capturing -> Int): The target function to benchmark.