I’ve got a relatively simple piece of code that takes an 2D array (“correlation”), and gives you a radial average working outwards from the centre.

xs = range(1, 100, 100)
ys = range(1, 100, 100)
R(x, y) = (sqrt((x-51)^2 + (y-51)^2))
autocorr_distances = R.(xs, ys')
@time begin
for r in range(1, 50, 50)
indexes = findall(x->(x<(r+0.5) && x>(r-0.5)), autocorr_distances)
sum = 0
for index in indexes
sum += correlation[index[1], index[2]]
end
radial_sum[trunc(Int,r)] = sum/length(indexes)
end
end

The code itself works fine and computes what I want it to, but when I time it, it yields:

0.171478 seconds (565.92 k allocations: 26.139 MiB, 98.28% compilation time)

The roughly 98% compilation time remains even when I run the code multiple times. I am a computer science and Julia beginner, so apologies if the answer is obvious!

Just for fun, I put your original code in a function:

function testit()
xs = range(1, 100, 100)
ys = range(1, 100, 100)
R(x, y) = (sqrt((x-51)^2 + (y-51)^2))
autocorr_distances = R.(xs, ys')
correlation = rand(100,100)
radial_sum = zeros(50)
@time begin
for r in range(1, 50, 50)
indexes = findall(x->(x<(r+0.5) && x>(r-0.5)), autocorr_distances)
sum = 0
for index in indexes
sum += correlation[index[1], index[2]]
end
radial_sum[trunc(Int,r)] = sum/length(indexes)
end
end
end

I had to add initialization for correlation and radial_sum which weren’t provided in your code. With this function definition I get the following output:

Roughly the same as your results with Statistics.mean. The moral is to beware of nonconstant globals.

I would also recommend replacing the range(1,50,50) with the simpler 1:50 which, when iterated, takes integer values, so that radial_sum[trunc(Int,r)] can be replaced by radial_sum[r]. Also, your variable name sum is not ideal, since there is a built-in sum function in Julia. Finally, since correlation is presumably a matrix of floating-point values, you should initialize summ (new name) to 0.0 (or even better to zero(eltype(correlation))) to avoid type instability.

Thank you so much for all of the advice, I really appreciate it! Your cautioning about nonconstant globals was key to solving a similar issue elsewhere in my code.