Hello. I recently tried out Julia because I was hoping for a speed improvement over numpy.sum(). I’m trying to benchmark a sum for a 2D integral and so far my Julia code is much slower. I’m wondering if there’s any performance tips I missed out on. My Julia code benchmark is this
function testfunction(my_arr,other_arr,sec_arr,third_arr)
for i in 1:length(my_arr)
new_array[i] = sum(@. 1 / (my_arr[i] * 1im + sec_arr + other_arr[i,1] ) * third_arr)
end
return new_array
end
which when I feed arrays that emulate my usecase ( the arithmetic operations to define the functions are arbitrary, it is just the length of arrays and approximate range that are important).
my_arr = collect(LinRange(0,10,2^15))
other_arr =Array{Float64}(undef, length(my_arr),2)
for i in 1:2
other_arr[:,i] = my_arr .* 2 .+ (5.3)
end
sec_arr = collect(LinRange(-2,2,600))
third_arr = exp.(sec_arr .+ 2) .* 3
new_array = Array{ComplexF64}(undef, length(my_arr))
@time testfunction(my_arr,other_arr,sec_arr,third_arr)
I get 0.628377 seconds (97.79 k allocations: 305.492 MiB, 3.70% gc time)
In Python, I tried a looped and vectorized method
import numpy as np
import time
def loopcompute(my_arr, other_arr,sec_arr,third_arr):
new_array = np.empty(len(my_arr), dtype = 'complex128')
for i in range(len(my_arr)):
new_array[i] = np.sum((my_arr[i]*1j+ sec_arr + other_arr[i,0] )**(-1)*third_arr)
return new_array
def vectcompute(my_arr, other_arr,sec_arr,third_arr):
new_array = np.sum((my_arr[:,None]*1j + sec_arr + other_arr[:,0][:,None])**(-1)*third_arr, axis = 1)
return new_array
and my test
my_arr = np.linspace(0,10,2**15)
other_arr = np.empty([len(my_arr),2])
for i in range(2):
other_arr[:,i] = my_arr*2+5.3
sec_arr = np.linspace(-2,2,600)
third_arr = np.exp(sec_arr+2)*3
t11 = time.time()
new_arr = vectcompute(my_arr, other_arr,sec_arr,third_arr)
t12 = time.time()
vectortime = t12 - t11
t21 = time.time()
new_arr2 = loopcompute(my_arr, other_arr,sec_arr,third_arr)
t22 = time.time()
looptime = (t22 - t21)
print(vectortime, looptime, vectortime / looptime)
gives 0.3255 and 0.4755 seconds respectively, a ~ 100% or ~ 75% improvement respectively. (the Python improvement does drop if the second/third array is much longer (>1000) ) but I’m hoping to get this down to 1/10th of a second or else I need to switch to C++. Is there any advice for optimizing the Julia script?