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
improving speed of factorial function #1170
Conversation
use divide and conquer to improve factorial for both number and bignumber
improving speed of bignumber and number
speed up factorial function for number
wow, that's impressive!!! Thanks a lot @honeybar . I think your work is ready to be merged as is, though I have a few remarks:
|
ah sorry, now I see. Forget about my point 3. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just a quick question 👍
if (n === i) { | ||
return n | ||
} | ||
half = ((n + i) / 2) | 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does | 0
round towards zero here? Maybe Math.trunc
would be clearer if so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, math.trunc is slower compared to operators that convert to integers. I decided to use the >> operator instead since I am gonna dividing by 2 and truncate anyways. I did leave a comment in product.js to clarify what it does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Thanks for the updates @honeybar . Just curious: have you also tried a plain old for loop to calculate the product, instead of the recursive solution that you have now? |
@josdejong I haven't tried converting the divide and conquer solution to for-loop. I think it might be possible, but it would be rather complex to do so, since divide and conquer is inherently recursive process. |
Thanks for your latest updates @honeybar! |
The improved function |
Hi, I modified factorial functions in gamma.js, permutation.js and combination.js for regular number to speed up the run time by modifying how product is calculated. (see: productrange.js for more info on how it is done). I tried to implement the same algorithm for big number but it ends up slower compared to the original, which is a bit of a surprise since, on regular number, the trend is that it will outperform the original method. I am still rather new to open source contribution so feedback is appreciated.
Thank you.
here is the log of performance for permutation function original vs new:
ans for original algorithm for 50P25: 1.9607814681608194e+39
2.525ms
ans for newer algorithm for 50P25: 1.9607814681608194e+39
0.187ms
here is one for combination function:
ans for original algorithm for 50C25: 126410606437752
11.590ms
ans for new algorithm for 50C25: 126410606437752
0.686ms
here is one for factorial:
ans for original algorithm for 50! : 3.0414093201713376e+64
2.647ms
ans for new algorithm for 50! : 3.0414093201713376e+64
0.703ms