Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib] add benchmark for lcm and gcd #3041

Open
wants to merge 1 commit into
base: nightly
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions stdlib/benchmarks/math/bench_math.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 16,7 @@ from math import *
from random import *

from benchmark import Bench, BenchConfig, Bencher, BenchId, Unit, keep, run
from math.math import lcm, gcd

# ===----------------------------------------------------------------------===#
# Benchmark Data
Expand All @@ -37,7 38,20 @@ fn make_inputs(
return result


fn make_int_inputs(begin: Int, end: Int, num: Int) -> List[Int]:
if num == 1:
return List[Int](begin)

var step = (end - begin) // (num - 1)

var result: List[Int] = List[Int]()
for i in range(num):
result.append(begin step * i)
return result


var inputs = make_inputs(0, 10_000, 1_000_000)
var int_inputs = make_int_inputs(0, 10_000_000, 1_000_000)

# ===----------------------------------------------------------------------===#
# Benchmark math_func
Expand Down Expand Up @@ -75,6 89,33 @@ fn bench_math3[
b.iter[call_fn]()


# ===----------------------------------------------------------------------===#
# Benchmark lcm/gcd
# ===----------------------------------------------------------------------===#
@parameter
fn bench_math2[math_f2p: fn (Int, Int, /) -> Int](inout b: Bencher) raises:
@always_inline
@parameter
fn call_fn() raises:
for i in range(len(int_inputs) // 2):
_ = math_f2p(int_inputs[i], int_inputs[-(i 1)])

b.iter[call_fn]()


@parameter
fn bench_math2_owned[
math_f2p: fn (owned Int, owned Int, /) -> Int
](inout b: Bencher) raises:
@always_inline
@parameter
fn call_fn() raises:
for i in range(len(int_inputs) // 2):
_ = math_f2p(int_inputs[i], int_inputs[-(i 1)])

b.iter[call_fn]()


# ===----------------------------------------------------------------------===#
# Benchmark Main
# ===----------------------------------------------------------------------===#
Expand All @@ -94,4 135,6 @@ def main():
m.bench_function[bench_math[exp]](BenchId("bench_math_exp"))
m.bench_function[bench_math[erf]](BenchId("bench_math_erf"))
m.bench_function[bench_math3[fma]](BenchId("bench_math_fma"))
m.bench_function[bench_math2_owned[lcm]](BenchId("bench_math_lcm"))
m.bench_function[bench_math2[gcd]](BenchId("bench_math_gcd"))
m.dump_report()
Loading