Skip to content

Conversation

@forfudan
Copy link
Owner

@forfudan forfudan commented Mar 8, 2025

This pull request includes several changes in the decimojo project, focusing on enhancing the multiply() function with UInt128 and UInt256. The performance of multiplication is significantly improved.

Enhancements to Decimal struct:

  • src/decimojo/decimal.mojo: Added raises keyword to __mul__ methods and introduced new methods is_uint32able and is_uint64able to check if the coefficient can be represented as UInt32 or UInt64 values. [1] [2] [3]

Improvements to rounding functionality:

Updates to utility functions:

  • src/decimojo/utility.mojo: Replaced number_of_significant_digits with number_of_digits, and added new functions truncate_to_digits and number_of_bits for better digit and bit manipulation. [1] [2] [3] [4]

Enhancements to testing:

  • tests/test_utility.mojo: Updated tests to reflect changes in utility functions, including new tests for truncate_to_digits and replacing tests for number_of_significant_digits with number_of_digits. [1] [2] [3] [4]

Additional changes:

  • mojoproject.toml: Added individual benchmark commands for bench_multiply.mojo and bench_divide.mojo.

@forfudan forfudan merged commit ac90d26 into main Mar 8, 2025
2 checks passed
@forfudan forfudan deleted the work branch March 8, 2025 21:25
@forfudan
Copy link
Owner Author

forfudan commented Mar 8, 2025

Bench before change:

=== DeciMojo Multiplication Benchmark ===
Time: 2025-03-08T12:14:12.410975
System: Darwin 24.3.0
Processor: arm
Python version: 3.12.9
Python decimal precision: 28
Mojo decimal precision: 28

Running multiplication benchmarks with 1000 iterations each

Benchmark:       Small integers
Mojo result:     408
Python result:   408
Mojo Decimal:    3.0 ns per iteration
Python Decimal:  3898.0 ns per iteration
Speedup factor:  1299.3333333333333

Benchmark:       Simple decimals
Mojo result:     69.9678
Python result:   69.9678
Mojo Decimal:    3.0 ns per iteration
Python Decimal:  3771.0 ns per iteration
Speedup factor:  1257.0

Benchmark:       Different scales
Mojo result:     56.088
Python result:   56.088
Mojo Decimal:    2.0 ns per iteration
Python Decimal:  3706.0 ns per iteration
Speedup factor:  1853.0

Benchmark:       Multiplication by zero
Mojo result:     0.00
Python result:   0.00
Mojo Decimal:    2.0 ns per iteration
Python Decimal:  3755.0 ns per iteration
Speedup factor:  1877.5

Benchmark:       Multiplication by one
Mojo result:     123.45
Python result:   123.45
Mojo Decimal:    3.0 ns per iteration
Python Decimal:  3897.0 ns per iteration
Speedup factor:  1299.0

Benchmark:       Negative numbers
Mojo result:     -69.9678
Python result:   -69.9678
Mojo Decimal:    3.0 ns per iteration
Python Decimal:  3645.0 ns per iteration
Speedup factor:  1215.0

Benchmark:       High precision multiplication
Mojo result:     0.1219326311370217861743636654
Python result:   0.1219326311370217861743636654
Mojo Decimal:    79.0 ns per iteration
Python Decimal:  3417.0 ns per iteration
Speedup factor:  43.25316455696203

Benchmark:       Large integer multiplication
Mojo result:     121932631112635269
Python result:   121932631112635269
Mojo Decimal:    2.0 ns per iteration
Python Decimal:  3466.0 ns per iteration
Speedup factor:  1733.0

Benchmark:       Fractional multiplication
Mojo result:     0.0001
Python result:   0.0001
Mojo Decimal:    3.0 ns per iteration
Python Decimal:  3663.0 ns per iteration
Speedup factor:  1221.0

Benchmark:       Powers of 10
Mojo result:     100
Python result:   100
Mojo Decimal:    3.0 ns per iteration
Python Decimal:  3787.0 ns per iteration
Speedup factor:  1262.3333333333333

=== Multiplication Benchmark Summary ===
Benchmarked:      10 different multiplication cases
Each case ran:    1000 iterations
Performance:      See detailed results above for each case

@forfudan
Copy link
Owner Author

forfudan commented Mar 8, 2025

Bench after change:

=== DeciMojo Multiplication Benchmark ===
Time: 2025-03-08T22:16:29.735935
System: Darwin 24.3.0
Processor: arm
Python version: 3.12.9
Python decimal precision: 28
Mojo decimal precision: 28

Running multiplication benchmarks with 1000 iterations each

Benchmark:       Small integers
Mojo result:     408
Python result:   408
Mojo Decimal:    6.0 ns per iteration
Python Decimal:  3549.0 ns per iteration
Speedup factor:  591.5

Benchmark:       Simple decimals
Mojo result:     69.9678
Python result:   69.9678
Mojo Decimal:    7.0 ns per iteration
Python Decimal:  3896.0 ns per iteration
Speedup factor:  556.5714285714286

Benchmark:       Different scales
Mojo result:     56.088
Python result:   56.088
Mojo Decimal:    6.0 ns per iteration
Python Decimal:  3533.0 ns per iteration
Speedup factor:  588.8333333333334

Benchmark:       Multiplication by zero
Mojo result:     0.00
Python result:   0.00
Mojo Decimal:    3.0 ns per iteration
Python Decimal:  3712.0 ns per iteration
Speedup factor:  1237.3333333333333

Benchmark:       Multiplication by one
Mojo result:     123.45
Python result:   123.45
Mojo Decimal:    4.0 ns per iteration
Python Decimal:  3709.0 ns per iteration
Speedup factor:  927.25

Benchmark:       Negative numbers
Mojo result:     -69.9678
Python result:   -69.9678
Mojo Decimal:    8.0 ns per iteration
Python Decimal:  3874.0 ns per iteration
Speedup factor:  484.25

Benchmark:       High precision multiplication
Mojo result:     0.1219326311370217861743636654
Python result:   0.1219326311370217861743636654
Mojo Decimal:    341.0 ns per iteration
Python Decimal:  3619.0 ns per iteration
Speedup factor:  10.612903225806452

Benchmark:       Large integer multiplication
Mojo result:     121932631112635269
Python result:   121932631112635269
Mojo Decimal:    7.0 ns per iteration
Python Decimal:  3733.0 ns per iteration
Speedup factor:  533.2857142857143

Benchmark:       Fractional multiplication
Mojo result:     0.0001
Python result:   0.0001
Mojo Decimal:    4.0 ns per iteration
Python Decimal:  3676.0 ns per iteration
Speedup factor:  919.0

Benchmark:       Powers of 10
Mojo result:     100
Python result:   100
Mojo Decimal:    6.0 ns per iteration
Python Decimal:  3900.0 ns per iteration
Speedup factor:  650.0

=== Multiplication Benchmark Summary ===
Benchmarked:      10 different multiplication cases
Each case ran:    1000 iterations
Performance:      See detailed results above for each case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants