Le dimanche 14 septembre 2014 à 12:30 -0700, Zac a écrit : > Hi I'm trying to port some c++ code to julia for approximating > functions on a sparse grid and have run into a strange 100x slow down > that I can't work > out. https://gist.github.com/Zac12345/3da7be1fe99681a5bd14 shows the > julia code and https://github.com/Zac12345/Sparse has the whole > module (though building the shared library can be a faff) > > > > Though the library uses multi-threading this tends to only give a 6-7x > speedup. > > > Is there anything obvious I'm missing out on here? > > > ps: profiling shows most of the time is spend (obviously) in the > innermost loop - line 39 of the gist - but a simple comparison of the > julia/c++ basis functions shows the julia version to be considerably > faster! I've just looked quickly at the code, but I've found two places where you are making copies of arrays, which hurts performance.
@inbounds w[1] = A[1] fill!(dA,w[1]) Aold += dA The last line is equivalent to Aold = Aold + dA, so it replaces Aold with the result of the addition. Given the code above, you can just do Aold += A[1]. @inbounds w[G.lvl_l[q]:G.lvl_l[q+1]-1]=A[G.lvl_l[q]:G.lvl_l[q+1]-1]-Aold[G.lvl_l[q]:G.lvl_l[q+1]-1] Here a copy of the extracted slice is taken. See the other thread "Why does this function incur so much garbage collection (and how can I make faster) ?". You should be able to use ArrayViews, SubArrays, or refactor your code. Aold += dA Same as the first issue. Better write this as a loop. Regards
