Skip to content

Commit 8359de6

Browse files
authored
[integer] Use slices operations on BigUInt fast division algorithm (#105)
1. Use slices operations on BigUInt fast division algorithm 2. Implement slices operations for other arithmetic operations where necessary.
1 parent 5911784 commit 8359de6

File tree

4 files changed

+602
-281
lines changed

4 files changed

+602
-281
lines changed

benches/biguint/bench_biguint_truncate_divide.mojo

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ fn run_benchmark_truncate_divide(
7373
speedup_factors: Mojo List to store speedup factors for averaging.
7474
"""
7575
log_print("\nBenchmark: " + name, log_file)
76-
log_print("Dividend: " + dividend, log_file)
77-
log_print("Divisor: " + divisor, log_file)
76+
log_print("Dividend: " + dividend[:1024] + "...", log_file)
77+
log_print("Divisor: " + divisor[:1024] + "...", log_file)
7878

7979
# Set up Mojo and Python values
8080
var mojo_dividend = BigUInt(dividend)
@@ -93,13 +93,21 @@ fn run_benchmark_truncate_divide(
9393
"Error: Mojo and Python results do not match!",
9494
log_file,
9595
)
96-
log_print("Mojo result: " + String(mojo_result), log_file)
97-
log_print("Python result: " + String(py_result), log_file)
96+
log_print(
97+
"Mojo result: "
98+
+ String(mojo_result)[:1024]
99+
+ String("..."),
100+
log_file,
101+
)
102+
log_print(
103+
"Python result: " + String(py_result)[:1024] + String("..."),
104+
log_file,
105+
)
98106
return # Skip this benchmark case if results don't match
99107

100108
# Display results for verification
101-
log_print("Mojo result: " + String(mojo_result), log_file)
102-
log_print("Python result: " + String(py_result), log_file)
109+
log_print("Mojo result: " + String(mojo_result)[:1024], log_file)
110+
log_print("Python result: " + String(py_result)[:1024], log_file)
103111

104112
# Benchmark Mojo implementation
105113
var t0 = perf_counter_ns()
@@ -164,7 +172,7 @@ fn main() raises:
164172
log_print("Could not retrieve system information", log_file)
165173

166174
var iterations = 100
167-
var iterations_large_numbers = 10
175+
var iterations_large_numbers = 2
168176

169177
# Define benchmark cases (all positive numbers for BigUInt)
170178
log_print(
@@ -406,7 +414,7 @@ fn main() raises:
406414

407415
# Case 24: Division of very large numbers
408416
run_benchmark_truncate_divide(
409-
"Division of very large numbers (2250 digits vs 900 digits)",
417+
"Division of very large numbers (250 words vs 100 words)",
410418
"123456789" * 250,
411419
"987654321" * 100,
412420
iterations,
@@ -502,7 +510,7 @@ fn main() raises:
502510

503511
# Case 31: Division of large numbers
504512
run_benchmark_truncate_divide(
505-
"Division of large numbers (5000 words vs words digits)",
513+
"Division of large numbers (5000 words vs 500 words)",
506514
"316227766_016824890_583648059_893174009_579947593" * 1000,
507515
"141421356_237309504_880168872_420969807_856967187" * 100,
508516
iterations_large_numbers,

pixi.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ c = "clear && pixi run clean"
2929
# tests (use the mojo testing tool)
3030
test = "pixi run package && pixi run mojo test tests -D ASSERT=all --filter"
3131
t = "clear && pixi run package && pixi run mojo test tests -D ASSERT=all --filter"
32+
b = "pixi run t big"
3233

3334
# benches
34-
bench_decimal = "clear && pixi run package && cd benches/decimal && pixi run mojo -I ../ bench.mojo && cd ../.. && pixi run clean"
35-
bench_bigint = "clear && pixi run package && cd benches/bigint && pixi run mojo -I ../ bench.mojo && cd ../.. && pixi run clean"
36-
bench_biguint = "clear && pixi run package && cd benches/biguint && pixi run mojo -I ../ bench.mojo && cd ../.. && pixi run clean"
37-
bench_bigdecimal = "clear && pixi run package && cd benches/bigdecimal && pixi run mojo -I ../ bench.mojo && cd ../.. && pixi run clean"
35+
bench_decimal = "clear && pixi run package && cd benches/decimal && pixi run mojo run -I ../ -D ASSERT=all bench.mojo && cd ../.. && pixi run clean"
36+
bench_bigint = "clear && pixi run package && cd benches/bigint && pixi run mojo run -I ../ -D ASSERT=all bench.mojo && cd ../.. && pixi run clean"
37+
bench_biguint = "clear && pixi run package && cd benches/biguint && pixi run mojo run -I ../ -D ASSERT=all bench.mojo && cd ../.. && pixi run clean"
38+
bench_bigdecimal = "clear && pixi run package && cd benches/bigdecimal && pixi run mojo run -I ../ -D ASSERT=all bench.mojo && cd ../.. && pixi run clean"
3839
bench_dec = "pixi run bench_decimal"
3940
bench_bint = "pixi run bench_bigint"
4041
bench_buint = "pixi run bench_biguint"

0 commit comments

Comments
 (0)