Thanks guys, and sorry for my late reply! So from the answers I get, that there is currently no fast generic way to do this? e.g. if I want to apply something other then norm column wise, I write a new function from scratch?
On Wednesday, August 12, 2015 at 5:56:08 PM UTC+2, David Gold wrote: > > Here's a way to do it with slices > > julia> function f(mat) > mat1 = slice(mat, 1, :) > mat2 = slice(mat, 2, :) > mat3 = slice(mat, 3, :) > A = Array(Float64, size(mat, 2)) > for i in 1:size(mat, 2) > A[i] = sqrt(mat1[i]^2 + mat2[i]^2 + mat3[i]^2) > end > A > end > f (generic function with 2 methods) > > julia> f(mat); > > julia> @time f(mat); > 0.008991 seconds (15 allocations: 7.630 MB) > > > On Wednesday, August 12, 2015 at 11:09:06 AM UTC-4, jw3126 wrote: >> >> I have a matrix and I want to compute the norm of each column. I tried >> >> using TimeIt >> N = 10^6 >> mat = randn(3, N) >> >> @timeit mapslices(norm, mat, 1) >> >> 1 loops, best of 3: >> >> 915.13 ms per loop >> >> >> Dog slow, 100times slower then numpy: >> >> import numpy as np >> N = 10**6 >> mat = np.random.randn(3, N) >> %timeit np.linalg.norm(mat, axis=0) >> >> >> 100 loops, best of 3: >> 9.71 ms per loop >> >> >> If I understand it correctly, the reason is that mapslices can not really >> optimize on the norm argument, e.g. inline norm. I had two ideas how to >> solve this problem, both of which are problematic: >> >> 1) There is the NumericExtensions >> <https://github.com/lindahua/NumericExtensions.jl> package, which seems >> to be aimed at this kind of problem, however this package is deprecated, so >> I don't want to use it. >> >> 2) Write some code by hand. This gives speedup, but is still slower then >> numpy: >> >> N = 10^6 >> mat = randn(3, N) >> function f(mat) >> return sqrt(mat[1,:].^2 + mat[2,:].^2 + mat[3,:].^2) >> end >> >> @timeit f(mat) >> >> 10 loops, best of 3: >> >> 38.51 ms per loop >> >> >> So how to make this computation fast? And more generally what are ways >> speed up mapslices without using NumericExtensions? >> >