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

Improve factorial(n, k) performance for BigInt #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

FedericoStra
Copy link

Use in-place operations from Base.GMP.MPZ to reduce allocations.

With this change I get a massive speed-up, for instance I pass from

julia> @benchmark factorial(big"1000", big"500")
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  100.233 μs … 105.019 ms  ┊ GC (min … max):  0.00% … 80.08%
 Time  (median):     116.240 μs               ┊ GC (median):     0.00%
 Time  (mean ± σ):   259.614 μs ±   2.368 ms  ┊ GC (mean ± σ):  20.95% ±  2.33%

  █▇▇▄▂▁                                                        ▂
  ███████▇▇▇█▅▅▃▃▃▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄ █
  100 μs        Histogram: log(frequency) by time        962 μs <

 Memory estimate: 192.52 KiB, allocs estimate: 3002.

to

julia> @benchmark factorial(big"1000", big"500")
BenchmarkTools.Trial: 10000 samples with 991 evaluations.
 Range (min … max):   49.491 ns … 92.862 μs  ┊ GC (min … max):  0.00% … 52.86%
 Time  (median):      54.742 ns              ┊ GC (median):     0.00%
 Time  (mean ± σ):   110.839 ns ±  2.088 μs  ┊ GC (mean ± σ):  23.97% ±  1.28%

   ▇█▆▁
  ▂█████▆▆▅▃▂▂▃▅▇▆▄▄▃▃▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
  49.5 ns         Histogram: frequency by time          105 ns <

 Memory estimate: 40 bytes, allocs estimate: 2.

for an approximate 2000x performance boost.

Use in-place operations from `Base.GMP.MPZ` to reduce allocations.
Copy link

codecov bot commented Dec 14, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (e6888be) 96.97% compared to head (dfb9766) 96.87%.

Files Patch % Lines
src/factorials.jl 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #149      +/-   ##
==========================================
- Coverage   96.97%   96.87%   -0.11%     
==========================================
  Files           7        7              
  Lines         728      736       +8     
==========================================
+ Hits          706      713       +7     
- Misses         22       23       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.

1 participant