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) |
@erasmospunk, Mark Roddy showed that xrange
and sum
instead 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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Without knowing much about Julia, I would guess that the
BigInt
constructor is taking a lot of the time; if you moved the constructor out of the loop, like what follows, it should be much faster (I realize this is only toy code, but it should indicate where the millisecs are going).My Julia build is broken at the moment, but the Forio web REPL indicates ~ half the time going to the constructor.