Last active
December 18, 2015 11:09
-
-
Save johnmyleswhite/5773873 to your computer and use it in GitHub Desktop.
Int's vs. BigInt's
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function foo(n::Integer) | |
| x::Int = 0 | |
| t1::Float64 = @elapsed for i in 1:n | |
| x = x + 1 | |
| end | |
| @printf "Int: %f\n" t1 | |
| y::BigInt = BigInt(0) | |
| t2::Float64 = @elapsed for i in 1:n | |
| y = y + BigInt(1) | |
| end | |
| @printf "BigInt: %f\n" t2 | |
| end | |
| foo(100_000_000) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@erasmospunk, Mark Roddy showed that
xrangeandsuminstead of a loop gives you a big speedup, but still not the full Julia speed. (In fairness, beating Julia here is close to impossible: the machine code for the loop is about as tight as you can get.)@shabbychef, You're right that the BigInt constructor is slowing this loop down a lot. I wrote things that way for simplicity, but this example shows that the Julia compiler is still not smart enough to fold many constants out during optimization. At the same time, I don't understand how the R and Python interpreters actually process these lines very well, so I wanted things to be as close to parallel as I knew how to make them.