Skip to content

Instantly share code, notes, and snippets.

@johnmyleswhite
Last active December 18, 2015 11:09
Show Gist options
  • Select an option

  • Save johnmyleswhite/5773873 to your computer and use it in GitHub Desktop.

Select an option

Save johnmyleswhite/5773873 to your computer and use it in GitHub Desktop.
Int's vs. BigInt's
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)
@johnmyleswhite
Copy link
Author

@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