Skip to content

Commit a33d891

Browse files
authored
[decimal] Optimize number_of_digit + improve the performance of multiply and exp (#31)
This pull request includes several changes across different files to optimize the performance of `multiply` and `exp` and improve the functionality and readability of the codebase. The changes involve importing and utilizing new modules, refactoring functions for better clarity, and fixing minor issues. This PR significantly improve the speed when multiplying two decimals with many digits after the decimal points (>20). This indirectly enhances the performance of `exp` significantly. ### Utility Functions: * Refactored the `truncate_to_max` function to use `power_of_10` for calculating powers of ten. * Optimize `number_of_digit` by using direct lookup instead of calculating ad hoc. ### Arithmetic Functions: * Refactored the `add` and `multiply` functions for better readability and efficiency. This includes using `Decimal.from_uint128` for creating `Decimal` objects and improving the handling of scale and rounding.
1 parent d30418e commit a33d891

File tree

7 files changed

+498
-166
lines changed

7 files changed

+498
-166
lines changed

.github/workflows/run_tests.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
os: ["macos-latest"]
17-
# os: ["ubuntu-22.04"]
16+
# os: ["macos-latest"]
17+
os: ["ubuntu-22.04"]
1818

1919
runs-on: ${{ matrix.os }}
2020
timeout-minutes: 30

benches/bench.mojo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ from bench_sqrt import main as bench_sqrt
66
from bench_from_float import main as bench_from_float
77
from bench_from_string import main as bench_from_string
88
from bench_comparison import main as bench_comparison
9+
from bench_exp import main as bench_exp
910

1011

1112
fn main() raises:
@@ -17,3 +18,4 @@ fn main() raises:
1718
bench_from_float()
1819
bench_from_string()
1920
bench_comparison()
21+
bench_exp()

benches/bench_exp.mojo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn run_benchmark(
7171
var mojo_decimal = Decimal(input_value)
7272
var pydecimal = Python.import_module("decimal")
7373
var py_decimal = pydecimal.Decimal(input_value)
74-
var py_math = Python.import_module("math")
74+
var _py_math = Python.import_module("math")
7575

7676
# Execute the operations once to verify correctness
7777
var mojo_result = dm.exponential.exp(mojo_decimal)

mojoproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ debug_sqrt = "magic run package && magic run mojo tests/test_sqrt.mojo && magic
3333
test = "magic run package && magic run mojo test tests && magic run delete_package"
3434
t = "clear && magic run test"
3535
test_arith = "magic run package && magic run mojo test tests/test_arithmetics.mojo && magic run delete_package"
36-
test_div = "magic run package && magic run mojo test tests/test_division.mojo && magic run delete_package"
36+
test_divide = "magic run package && magic run mojo test tests/test_division.mojo && magic run delete_package"
3737
test_sqrt = "magic run package && magic run mojo test tests/test_sqrt.mojo && magic run delete_package"
3838
test_round = "magic run package && magic run mojo test tests/test_round.mojo && magic run delete_package"
3939
test_creation = "magic run package && magic run mojo test tests/test_creation.mojo && magic run delete_package"
@@ -46,8 +46,8 @@ test_exp = "magic run package && magic run mojo test tests/test_exp.mojo && magi
4646
# benches
4747
bench = "magic run package && cd benches && magic run mojo bench.mojo && cd .. && magic run delete_package"
4848
b = "clear && magic run bench"
49-
bench_mul = "magic run package && cd benches && magic run mojo bench_multiply.mojo && cd .. && magic run delete_package"
50-
bench_div = "magic run package && cd benches && magic run mojo bench_divide.mojo && cd .. && magic run delete_package"
49+
bench_multiply = "magic run package && cd benches && magic run mojo bench_multiply.mojo && cd .. && magic run delete_package"
50+
bench_divide = "magic run package && cd benches && magic run mojo bench_divide.mojo && cd .. && magic run delete_package"
5151
bench_sqrt = "magic run package && cd benches && magic run mojo bench_sqrt.mojo && cd .. && magic run delete_package"
5252
bench_round = "magic run package && cd benches && magic run mojo bench_round.mojo && cd .. && magic run delete_package"
5353
bench_from_float = "magic run package && cd benches && magic run mojo bench_from_float.mojo && cd .. && magic run delete_package"

0 commit comments

Comments
 (0)