In the following function, I see 1.56X speedup when using zeros()
function for one input instead of using Array{T}()
, which seems odd to me. When I use @btime
though, I get the same timings.
julia> function vander(v, x, N::Int)
M = length(x)
if N > 0
v[:,1] .= 1
end
if N > 1
for i = 2:N
v[:,i] = x
end
accumulate(v, v)
end
return v
end
vander (generic function with 1 method)
julia> function accumulate(input, output)
M, N = size(input)
for i = 2:N
for j = 1:M
output[j,i] *= input[j,i-1]
end
end
end
accumulate (generic function with 1 method)
Now compare the timings of f()
and g()
:
julia> function f()
M, N = 10^8, 4
x = rand(M)
v = Array{Float64}(undef,M,N) # <-----
t = @elapsed vander(v, x, N)
end
f (generic function with 1 method)
julia> f()
1.2380960570000001
julia> function g()
M, N = 10^8, 4
x = rand(M)
v = zeros(M,N) # <-----
t = @elapsed vander(v, x, N)
end
g (generic function with 1 method)
julia> g()
0.77949281