4 ms·
Huh? Can you provide an example? Well he did mention numpy and obviously doing something that can be done in numpy with nested loops will be much much slower.
by dagw 10y ago
Huh? Can you provide an example?
Well he did mention numpy and obviously doing something that can be done in numpy with nested loops will be much much slower. However that is as much a case of numpy being really fast as python being slow.
For example, just tested elementwise multiplication of two 10kX10k matrices and with numpy and numpy arrays it took ~350 ms vs ~15 seconds with a nested for loops and python lists. Still minutes vs. milliseconds seems like extreme hyperbole.
edit: Figured I'd test how long the naive nested loop approach would take in a 'fast' language like Julia, and much to my surprise it took over 50 seconds.
edit2: cleaned up all my code to try to make it as similar as possible across both languages:
Nested for loops python 2.7: ~30 seconds
Nested for loops Julia 4.6; ~50 seconds
Nested for loops PyPy 5.3.1 ~0.9 seconds
Make of these numbers what you will
- ced 10y agoCould you post the Julia code? Unfortunately, you do have to make sure that the types are correctly inferred by the compiler in high-performance loops. Or maybe you used a global. http://docs.julialang.org/en/release-0.4/manual/performance-tips/ http://docs.julialang.org/en/release-0.4/manual/performance-...
- dagw 10y agoI'll admit I don't really know that much Julia and basically wrote naive MATLAB code (and I wanted to make it as close as possible to my python code): s=10000; a=ones(s,s); b=ones(s,s); c=zeros(s,s); tic(); for i in 1:s for j in 1:s c[i,j]=a[i,j]*b[i,j] end end toc(); Obviously in real code I'd simple write c=a.*b and get basically the same performance as numpy
- Tarrosion 10y agoIt looks like this was done at global scope. It's important to wrap things in a function for maximum performance - AFAIK, because method dispatch and precompilation happen at the function level, functions are much much faster than not functions. Julia also stores its arrays column major, so put the outer loop over columns for another order of magnitude performance increase. (I'm not sure I love this feature, but presumably there's a reason...maybe for better performance on matrix operations?) My reply to your grandparent comment has a simple implementation that runs in 0.4 seconds on an aging machine.
- ced 10y agoTarrosion is right, on my machine, putting the code inside a function is 10X faster, and then swapping the order of the loops gives another 10X improvement: function f(s) a=ones(s,s); b=ones(s,s); c=zeros(s,s); tic(); for j in 1:s for i in 1:s c[i,j]=a[i,j]*b[i,j] end end toc(); return c end Note that you also might have to run the function twice, because the first time JIT-compilation kicks in. Julia 0.5 also now automatically devectorizes some code (eg. x = a .* b .+ c) so that you don't have to write explicit loops to get performance benefits.
- dagw 10y agoThanks. To both you and Tarrosion. Guess I need to take some time at some point to sit down and actually learn 'proper' Julia.
- shakna 10y agoBased purely on my experience with native CPython, I have usually found for loops to be a bottleneck - and nested list comprehensions to be the saviour. I wonder what the difference would look like then?
- Tarrosion 10y agoI've been using Julia a lot lately so these results really surprised me, and I wrote my own test. Punchline: it takes about 0.4 seconds on Julia 0.4.6, on my machine which is several years old. Check it out: julia> include("elmult.jl") eachindex 0.42587028 outer loop over rows 8.923323015 outer loop over cols 0.516270241 Code: function elmult(m1, m2) result = zeros(m1) for i in eachindex(m1) result[i] = m1[i] * m2[i] end return result end function elmult_rowmajor(m1, m2) result = zeros(m1) nr = size(m1, 1) nc = size(m1, 2) for i in 1:nr, j in 1:nc result[i,j] = m1[i,j] * m2[i,j] end return result end function elmult_colmajor(m1, m2) result = zeros(m1) nr = size(m1, 1) nc = size(m1, 2) for j in 1:nc, i in 1:nr result[i,j] = m1[i,j] * m2[i,j] end return result end elmult(rand(3,3), rand(3,3)) elmult_colmajor(rand(3,3), rand(3,3)) elmult_rowmajor(rand(3,3), rand(3,3)) m1, m2 = rand(10000,10000), rand(10000, 10000); println("eachindex") println(@elapsed elmult(m1, m2)) println("outer loop over rows") println(@elapsed elmult_rowmajor(m1, m2)) println("outer loop over cols") println(@elapsed elmult_colmajor(m1, m2))