Hi everybody,
I wanted to quick benchmark the BigInt type. I wrote a simple function (with double reccursion just because I want to consume time ). I then tested the same function in Python, and found that Python 12 is 10 times faster on this example.
Here is my Julia 1.9.4 code:
nfib(n) = n > 2 ? (nfib(n-2)+nfib(n-1))*n : n
println("--- First run ---")
@time x = nfib(big(40))
println(x)
println("--- Second run ---")
@time x = nfib(big(40))
println(x)
Here is my Python 12 code:
import time
def nfib(n):
return (nfib(n-2)+nfib(n-1))*n if n > 2 else n
print(f" --- First run ---")
t = time.perf_counter()
x = nfib(40)
print("Time=", time.perf_counter()-t, "seconds")
print(x)
print(f" --- Second run ---")
t = time.perf_counter()
x = nfib(40)
print("Time=", time.perf_counter()-t, "seconds")
print(x)
Julia 1.9.4 result:
ā First run ā
97.582200 seconds (1.74 G allocations: 33.548 GiB, 27.86% gc time)
12306496796223503426182275975073985352177589230680
ā Second run ā
97.781208 seconds (1.74 G allocations: 33.548 GiB, 28.93% gc time)
12306496796223503426182275975073985352177589230680
Python 12 result:
ā First run ā
Time= 8.463558899995405 seconds
12306496796223503426182275975073985352177589230680
ā Second run ā
Time= 8.873872700001812 seconds
12306496796223503426182275975073985352177589230680
I did not expect such a difference !
I made some progress by adding two constants in my code:
const bigone = big(1)
const bigtwo = big(2)
nfib(n) = n > bigtwo ? (nfib(n-bigtwo)+nfib(n-bigone))*n : n
With the following result:
ā First run ā
57.288087 seconds (921.01 M allocations: 18.299 GiB, 23.61% gc time)
12306496796223503426182275975073985352177589230680
ā Second run ā
57.313774 seconds (921.01 M allocations: 18.299 GiB, 24.87% gc time)
12306496796223503426182275975073985352177589230680
But, it is still 5 time slower than the same Python code.
Do you have other ideas for improvement ?