There is a function in my main program and I have realised that this function consumes most of time in my program. Is it possible to speed up this mentioned function given below
function fun1(r::Matrix,od::Int64,γ::Float64,e::Float64)
n = size(r)[1]; p = zero(r);
if od == 3
for j in 1:n
for i in 1:n
p[i,j] = r[i,j]^3*γ + exp(-(r[i,j]*e)^2)
end
end
elseif od == 5
for j in 1:n
for i in 1:n
p[i,j] = r[i,j]^5*γ + exp(-(r[i,j]*e)^2)
end
end
elseif od == 7
for j in 1:n
for i in 1:n
p[i,j] = r[i,j]^7*γ + exp(-(r[i,j]*e)^2)
end
end
else
println("enough")
end
return p
end
updated codes, after answer of tamasgal
using Printf
using BenchmarkTools
function fun2(r::Matrix, od::Int64, γ::Float64, e::Float64)
n = size(r)[1]; p = zero(r);
for j in 1:n
for i in 1:n
p[i,j] = r[i,j]^od*γ + exp(-(r[i,j]*e)^2)
end
end
return p
end
I used @simd
before for loops and @inbounds
before p[i,j]
, but no significant difference in speed. On the other using @fastmath gives some improvement however the results are not correct always. Also used Profiler and @code_warntype but I could not achive any improvement.
Thank you.
Elapsed time for me:
@benchmark fun1(rand(150,150),7,2.0,1.0)
BenchmarkTools.Trial:
memory estimate: 175.89 KiB
allocs estimate: 2
--------------
minimum time: 1.251 ms (0.00% GC)
median time: 1.269 ms (0.00% GC)
mean time: 1.281 ms (0.16% GC)
maximum time: 2.449 ms (35.99% GC)
--------------
samples: 3897
evals/sample: 1