Skip to content

Commit

Permalink
[External] [stdlib] Add benchmark for lcm and gcd (#41811)
Browse files Browse the repository at this point in the history
[External] [stdlib] Add benchmark for `lcm` and `gcd`

Add benchmarks for `lcm` and `gcd` functions.

Co-authored-by: bgreni <[email protected]>
Closes #3041
MODULAR_ORIG_COMMIT_REV_ID: 3fe9e4a6321832878a49ad79d90593ef2969076b
  • Loading branch information
bgreni authored and modularbot committed Dec 17, 2024
1 parent ef488e0 commit 85d204c
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions stdlib/benchmarks/math/bench_math.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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 @@ -79,6 +92,21 @@ 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):
var result = keep(math_f2p(int_inputs[i], int_inputs[-(i + 1)]))
keep(result)

b.iter[call_fn]()


# ===----------------------------------------------------------------------===#
# Benchmark Main
# ===----------------------------------------------------------------------===#
Expand All @@ -98,4 +126,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[lcm]](BenchId("bench_math_lcm"))
m.bench_function[bench_math2[gcd]](BenchId("bench_math_gcd"))
m.dump_report()

0 comments on commit 85d204c

Please sign in to comment.