mojo benchmark

benchmark

Implements the benchmark module for runtime benchmarking.

You can import these APIs from the benchmark package. For example:

import benchmarkfrom time import sleep

You can pass any fn as a parameter into benchmark.run[...](), it will return a Report where you can get the mean, duration, max, and more:

fn sleeper():    sleep(.01)var report = benchmark.run[sleeper]()print(report.mean())
0.012256487394957985

You can print a full report:

report.print()
---------------------Benchmark Report (s)---------------------Mean: 0.012265747899159664Total: 1.459624Iters: 119Warmup Mean: 0.01251Warmup Total: 0.025020000000000001Warmup Iters: 2Fastest Mean: 0.0121578Slowest Mean: 0.012321428571428572

Or all the batch runs:

report.print_full()
---------------------Benchmark Report (s)---------------------Mean: 0.012368649122807017Total: 1.410026Iters: 114Warmup Mean: 0.0116705Warmup Total: 0.023341000000000001Warmup Iters: 2Fastest Mean: 0.012295586956521738Slowest Mean: 0.012508099999999999Batch: 1Iterations: 20Mean: 0.012508099999999999Duration: 0.250162Batch: 2Iterations: 46Mean: 0.012295586956521738Duration: 0.56559700000000002Batch: 3Iterations: 48Mean: 0.012380562499999999Duration: 0.59426699999999999

If you want to use a different time unit you can bring in the Unit and pass it in as an argument:

from benchmark import Unitreport.print(Unit.ms)
---------------------Benchmark Report (ms)---------------------Mean: 0.012312411764705882Total: 1.465177Iters: 119Warmup Mean: 0.012505499999999999Warmup Total: 0.025010999999999999Warmup Iters: 2Fastest Mean: 0.012015649999999999Slowest Mean: 0.012421204081632654

The unit’s are just aliases for StringLiteral, so you can for example:

print(report.mean("ms"))
<
  • 20
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

启航学途

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值