Skip to main content

Mojo struct

Bencher

@register_passable

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

Methods

__init__

__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](inout 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](inout 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](inout 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](inout 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](inout 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](inout 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](inout self)

Times a target function with custom number of iterations.

Parameters:

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