SageMath, Cython and Julia: BBP algorithm
Kernel: Julia
Julia version of BBP
Experiments with BBP algorithm in Julia.
First version
In [27]:
Out[27]:
digpi (generic function with 1 method)
th hexadecimal digit of (after the decimal point).
In [28]:
Out[28]:
2
A few more digits...
In [29]:
Out[29]:
6-element Array{Int64,1}:
2
6
12
6
5
14
How fast it is? To measure the performance we will use BenchmarkTools.jl.
In [30]:
Take .
In [32]:
Out[32]:
BenchmarkTools.Trial:
samples: 76
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 0.00 bytes
allocs estimate: 0
minimum time: 65.35 ms (0.00% GC)
median time: 65.45 ms (0.00% GC)
mean time: 65.80 ms (0.00% GC)
maximum time: 70.41 ms (0.00% GC)
Faster version
It is possible to speed things up a little bit (but not too much).
In [33]:
Out[33]:
digpi2 (generic function with 1 method)
In [34]:
Out[34]:
2
In [35]:
Out[35]:
6-element Array{Int64,1}:
2
6
12
6
5
14
Again, speed test for .
In [36]:
Out[36]:
BenchmarkTools.Trial:
samples: 165
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 0.00 bytes
allocs estimate: 0
minimum time: 29.49 ms (0.00% GC)
median time: 29.62 ms (0.00% GC)
mean time: 30.31 ms (0.00% GC)
maximum time: 41.13 ms (0.00% GC)
This is twice as fast as the previous Julia version. It is a little bit slower then the Cython version.
Parallel version
In [39]:
Out[39]:
digpi3 (generic function with 1 method)
In [37]:
Out[37]:
4
In [8]:
Out[8]:
1-element Array{Int64,1}:
5
In [38]:
Out[38]:
4
In [40]:
In [41]:
Out[41]:
8
In [42]:
Out[42]:
BenchmarkTools.Trial:
samples: 248
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 34.16 kb
allocs estimate: 593
minimum time: 15.64 ms (0.00% GC)
median time: 20.72 ms (0.00% GC)
mean time: 20.22 ms (0.00% GC)
maximum time: 26.43 ms (0.00% GC)
Test on larger input.
In [43]:
Out[43]:
BenchmarkTools.Trial:
samples: 3
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 34.88 kb
allocs estimate: 619
minimum time: 2.09 s (0.00% GC)
median time: 2.15 s (0.00% GC)
mean time: 2.16 s (0.00% GC)
maximum time: 2.25 s (0.00% GC)
In [44]:
Out[44]:
BenchmarkTools.Trial:
samples: 2
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
memory estimate: 0.00 bytes
allocs estimate: 0
minimum time: 4.54 s (0.00% GC)
median time: 4.60 s (0.00% GC)
mean time: 4.60 s (0.00% GC)
maximum time: 4.66 s (0.00% GC)