Several people on the discourse have recently suggested I use views to improve my code’s performance. However, for some reason, it actually slows down my code when I use them. To test this, I wrote the following MWE:
using FFTW
function with_views(N, M)
ψ = Array{Complex{Float64}}(undef, M, N)
ψ[1, :] = rand(N) + im*rand(N)
F = plan_fft!(@view ψ[1, :]) # Plan
F̃ = plan_ifft!(@view ψ[1, :]) # Plan
W = ifftshift(cis.(rand(512)))
for i = 1:M-1
@views ψ[i+1, :] = T2(ψ[i,:],W, 1.0/M, F, F̃)
end #for
return nothing
end #solve
function without_views(N, M)
ψ = Array{Complex{Float64}}(undef, M, N)
ψ[1, :] = rand(N) + im*rand(N)
F = plan_fft!(ψ[1, :]) # Plan
F̃ = plan_ifft!(ψ[1, :]) # Plan
W = ifftshift(cis.(rand(512)))
for i = 1:M-1
ψ[i+1, :] = T2(ψ[i,:],W, 1.0/M, F, F̃)
end #for
return nothing
end #solve
function T2(ψ, W, dx, F, F̃)
@inbounds for i in 1:length(ψ)
ψ[i] *= cis(dx/2 * (-1*abs2(ψ[i])))
end
F*ψ
@inbounds for i in 1:length(W)
ψ[i] *= W[i]
end
F̃*ψ
@inbounds for i in 1:length(ψ)
ψ[i] *= cis(dx/2 * (-1*abs2(ψ[i])))
end
return ψ
end #T2
@benchmark with_views(512, 10000)
@benchmark without_views(512, 10000)
Now to bench mark:
@benchmark with_views(512, 10000)
BenchmarkTools.Trial:
memory estimate: 157.51 MiB
allocs estimate: 10072
--------------
minimum time: 227.541 ms (1.81% GC)
median time: 235.816 ms (4.57% GC)
mean time: 241.442 ms (6.53% GC)
maximum time: 302.875 ms (25.67% GC)
--------------
samples: 21
evals/sample: 1
And without views:
@benchmark without_views(512, 10000)
BenchmarkTools.Trial:
memory estimate: 157.53 MiB
allocs estimate: 10074
--------------
minimum time: 165.236 ms (1.71% GC)
median time: 172.742 ms (6.32% GC)
mean time: 176.176 ms (8.19% GC)
maximum time: 236.072 ms (31.66% GC)
--------------
samples: 29
evals/sample: 1
I am not quite sure what’s going on here. Any advice?