3 ms·
It's hard to evalutate this comparison because the original Julia code isn't posted – who knows if it was well written or not? However, the same vectorization a
by StefanKarpinski 12y ago
It's hard to evalutate this comparison because the original Julia code isn't posted – who knows if it was well written or not? However, the same vectorization approach presented in R can also be used in Julia (or Python or Matlab):
julia> rw(n) = cumsum(2randbool(n) .- 1)
julia> @time rw(1000000)
elapsed time: 0.02370671 seconds (25125400 bytes allocated)
1000000-element Array{Int64,1}:
1
2
1
⋮
-314
-313
-314
Since there's no timings or comparison code in the article, we can't really compare performance beyond this. There is, however, this statement:
> This vectorized R code turned out to be much faster than the Julia code – more than 1000 times faster, in fact, in the case of simulation 1000000 steps. For 100000000 steps, Julia actually is much faster than R, but the point is that the claims made about Julia’s speed advantage are really overblown.
It's a little odd to argue that "it’s very unlikely that ... Julia will become more popular than R among data scientists" while completely dismissing Julia being "much faster than R" for bigger problems. This only makes sense if one assumes that addressing bigger problems is not of increasing importance in data science – which seems counterfactual, to say the least.
"R’s speedy vectorization features" mentioned in the article are not actually features – they are limitations. In R (and Python and Matlab), you have to write vectorized code because using a for loop is slow. In Julia, you can choose which approach is better for you and for the problem at hand. Want to write vectorized code? Not a problem. Want to use a for loop? Also not a problem. Want to use recursion? Yes, even that is ok.
I also find it interesting that the poster of this article is self-described as an "octave core dev" ;-)
- StefanKarpinski 12y agoI decided to do some timing comparisons against the given R implementation on my system so that we have some numbers. The minimum time rw(1000000) takes in R is 0.02 seconds – sometimes it's 2x or 3x that, probably because GC kicks in. The vectorized Julia version above takes 0.01 seconds minimum – twice as fast as R – but often it's 2.5x slower than that, also because of GC. Here's a stab at what an obvious iterative random walk implementation might look like in Julia: function rwi(n) a = Array(Int,n) s = 0 for i = 1:n s += ifelse(randbool(), -1, 1) a[i] = s end return a end rwi(1000000) takes 0.005 seconds. That's twice as fast as the vectorized Julia version and 4x faster than R. It also allocates much less memory than either one – just the output array. For 100000000 steps, R takes 2.717 seconds, the vectorized Julia version takes 1.52 seconds, and the iterative Julia version takes 0.6 seconds.