20M digits of pi in 1 minute using Julia
gist.github.com
gist.github.com
https://docs.swift.org/swift-book/documentation/the-swift-pr...
Not that I like doing this in Matlab, but that's what everyone uses, though some are moving to python.
help?> Ψ₂ˣ
"Ψ₂ˣ" can be typed by \Psi<tab>\_2<tab>\^x<tab>(You can also type these easily via latex-style completions, e.g. with \cup<TAB> for ∪ or \cap<TAB> for ∩)
Edit: also, reading https://stackoverflow.com/a/67919533, it seems you can do
BigFloat(π, precision=20000000)
How does that perform?julia> @time BigFloat(π, precision=20000000); 11.208211 seconds (60.53 k allocations: 2.932 GiB, 0.16% gc time)
Which is pretty good considering I ran this in WSL on my laptop and Mathematica in a different comment took 10 seconds. (Plain Windows took ~26 seconds for some reason?)
Maybe the allocations were the reason?
What value does that print?
Timing[N[\[Pi], 20000000];]
{10.1094, Null}
On a fairly recent laptop CPU I got 7.6 seconds.A fair ways to go still!
As a random example: large matrix operations have been multi-threaded for ages.
I believe JET.jl can automate global program analysis, but I haven't used it.
The world record remains at just under 4s, with 8GHz+ oerclocks: https://hwbot.org/benchmark/superpi_-_1m/halloffame
Clock cycle efficiency doesn't seem to have improved much in the last decade.
(define (pi digits)
(let* ((A 13591409)
(B 545140134)
(C 640320)
(C^3/24 (quotient (expt 640320 3) 24))
(D 12))
(define (-1^n*g n g) (if (odd? n) (- g) g))
(define (split m n)
(if (= 1 (- n m))
(let* ((6n (* 6 n)) (g (* (- 6n 5) (- (+ n n) 1) (- 6n 1))))
(list g (* C^3/24 (expt n 3)) (* (-1^n*g n g) (+ (* n B) A))))
(let* ((mid (quotient (+ m n) 2))
(gpq1 (split m mid))
(gpq2 (split mid n))
(g1 (car gpq1)) (p1 (cadr gpq1)) (q1 (caddr gpq1))
(g2 (car gpq2)) (p2 (cadr gpq2)) (q2 (caddr gpq2)))
(list (* g1 g2) (* p1 p2) (+ (* q1 p2) (* q2 g1))))))
(let* ((num-terms (inexact->exact (floor (+ 2 (/ digits 14.181647462)))))
(sqrt-C (integer-sqrt (* C (expt 100 digits))))
(gpq (split 0 num-terms))
(g (car gpq)) (p (cadr gpq)) (q (caddr gpq)))
(quotient (* p C sqrt-C) (* D (+ q (* p A)))))))
I am surprised it is much slower in Julia (as per what is noted in the gist).[1] https://www.youtube.com/watch?v=8RONJPOgZjw
[2] https://github.com/R-e-t-u-r-n-N-u-l-l/Java-Algorithms/blob/...
Julia also supports transparent Cluster API and CUDA integration. i.e. the hot-mess you hope to never have to maintain professionally.
In my humble opinion, the utility of this high-level language makes it fundamentally different. Thus, it is slowly becoming more mainstream as the ecosystem stabilizes.
Remember to have fun =)
You can have "very high level functional programming" and 8x the speed of Julia in a mathematics-oriented language!
The danger of course is when Wolfram takes those assumptions into a room of mathematicians. It is controversial for all the wrong reasons. =)
It is a common question by the way =)
https://stackoverflow.com/questions/60121757/julia-vs-mathem...
The reason Mathematica is so much faster here is it’s using a different algorithm. When you compare using the same algorithm, Julia is 10-100x faster than Mathematica. https://julialang.org/benchmarks/
Yet a polyglot project is bad design, and tends to become an abomination in time. This is one reason many Go programmers rewrote libraries rather than saturate their code with cgo/C and SWIG/C++ wrappers. Similarly, people are writing scalable versions of libraries in Julia to improve transparent parallel performance for problems too big for a single machine to feasibly handle.
Things are still undergoing change, but of course the commercial nvidia binary blobs will haunt everyone for awhile. =)
x*(y.^2)
To the Python equivalent:
np.matmul(x, map(lambda x : x^2, y))
Note also that the first will be much faster because it can fuse the matrix multiplication with the exponentiation. That’s because Julia is a single language, and the whole thing is compiled at the same time. Python can’t fix the above code because NumPy is written in C, not Python.
Julia is just Python but fast.
http://ewams.net/?date=2020/04/17&view=sockets_vs_cores_for_...
Though that is with a broadwell CPU and about 3 years ago. Might see if I can get my hands on a 6348 ice lake CPU and see how she burns.
So 40 digits of pi would indeed be in the ballpark for computing anything distance-wise at the circumference of the observable universe down to the hydrogen electron cloud size.
[0] https://www.jpl.nasa.gov/edu/news/2016/3/16/how-many-decimal... (Right before the last inline image)
but this is a joke hahaha