I have a very long function depends on X,Y,T. X and Y are vectors with length between 400-10000. I have to calculate the function in a for loop many times. Are my approachs efficient? If not, how can I improve them? Thank you for your help.
First approach:
using Printf
using BenchmarkTools
using Profile
using LoopVectorization
F(X, Y, T) = @. 15*(T^2/2+1)*exp(1*im*T)*(1-X)*sin(X)*(1-Y)*sin(Y)*(exp(T)*β*sin(X*π)^2*sin(Y*π)^2+225*(T^2/2+1)^2*(X-1)^2*sin(X)^2*(Y-1)^2*sin(Y)^2)+(-30*(T^2/2+1)*exp(im*T)*(1-X)*sin(X)*(1-Y)*sin(Y)-30*(T^2/2+1)*exp(im*T)*cos(X)*(1-Y)*sin(Y)-30*(T^2/2+1)*exp(im*T)*(1-X)*sin.(X)*cos(Y))*γ+(-15*(T^2/2+1)*exp(im*T)*sin(X)*(1-Y)*sin(Y)+15*(T^2/2+1)*exp(im*T)*(1-X)*cos(X)*(1-Y)*sin(Y)-15*(T^2/2+1)*exp(im*T)*(1-X)*sin(X)*sin(Y)+
15*(T^2/2+1)*exp(im*T)*(1-X)*sin(X)*(1-Y)*cos(Y))*δ+im*(15*im*(T^2/2+1)*exp(im*T)*(1-X)*sin(X)*(1-Y)*sin(Y)+15*T*exp(im*T)*(1-X)*sin(X)*(1-Y)*sin(Y))
const δ, γ, β = 1.0, 1.0, 1.0
function test(N,Δt)
# time parameters
#Δt = 0.00001
data = rand(N,2)
x = data[:,1]; y = data[:,2]
NN = Int(round(1/Δt))
for i in 1:NN
f = F(x,y,i*Δt)
#some code that uses f
end
end
Second approach:
function test2(N,Δt)
# time parameters
#Δt = 0.00001
data = rand(N,2)
x = data[:,1]; y = data[:,2]
f = Array{Complex{Float64}}(undef,N)
NN = Int(round(1/Δt))
@inbounds for i in 1:NN
@inbounds for j in 1:N
f[j] = F(x[j],y[j],i*Δt)
end
#some code that uses f
end
end
and timings:
@benchmark test(1600,0.0001)
BenchmarkTools.Trial:
memory estimate: 250.45 MiB
allocs estimate: 70004
--------------
minimum time: 4.349 s (0.16% GC)
median time: 4.372 s (0.15% GC)
mean time: 4.372 s (0.15% GC)
maximum time: 4.395 s (0.13% GC)
--------------
samples: 2
evals/sample: 1
@benchmark test2(1600,0.0001)
BenchmarkTools.Trial:
memory estimate: 75.41 KiB
allocs estimate: 6
--------------
minimum time: 3.863 s (0.00% GC)
median time: 3.868 s (0.00% GC)
mean time: 3.868 s (0.00% GC)
maximum time: 3.872 s (0.00% GC)
--------------
samples: 2
evals/sample: 1