Description
Hi,
I often want to add together two arrays with N small dimensions, where e.g. N=18 and each dimension has size 2.
N = 18
a = rand(2^N)
ar = reshape(a, ntuple(x->2, N))
Initially, I was unknowingly adding ReshapedArray objects with these dimensions, which is very slow, at least done naively using +
:
v = view(a, 1:2^N)
@time v + v #a few ms for N=18
vr = reshape(v, ntuple(x->2, N))
@time vr + vr #around 30s(!) for N=18
I see a similar slowdown with just Arrays if I try to use broadcasting.
@time a .+ a #a few ms
@time ar .+ ar #around 30s again!
Could these just be extreme cases of the following simple case?:
@time a + a #a few hundred ns
@time ar + ar #around 65ms (around 100 times slower)
I thought it might be due to linear indexing not being used, buy my own attempt to use it
function myplus(x, y)
res = zeros(x)
@inbounds for j in 1:length(x)
res[j] = x[j] + y[j]
end
end
@time myplus(ar,ar) #around 40 ms
was not much better, and obviously skips some safety checks.
This is with
Julia Version 0.6.0-dev.1230
Commit ce6a0c3 (2016-11-10 21:09 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Xeon(R) CPU @ 2.60GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
LAPACK: libopenblas64_
LIBM: libopenlibm
LLVM: libLLVM-3.7.1 (ORCJIT, sandybridge)
Julia 0.5.0 performs similarly, except that a .+ a
and a + a
switch speeds, with a .+ a
being faster (hundreds of ns) on 0.5.0.
Activity