Hi!
Really like julia and want to learn how to debug performance better.
I’m doing a project on iterated maps, and started writing in python and then decided to try julia. But when benchmarking, the python version ran faster, but I can’t figure out why.
The question about what to vectorize, when to use dot notation etc in julia is not entirely clear to me, if you have any general advice about this I’d be happy to receive it.
This is the vectorized python code
import numpy as np
def henon(x, a=1.4, b=0.3):
x = np.array(x)
x, y = x[:, 0], x[:, 1]
return np.stack([1-a*x**2+y, b*x], -1)
# in IPython REPL
# 10 000 dummy starting points
y = 0.34 * np.ones((10000, 2))
# %%time
for _ in range(10000):
y = henon(y)
#CPU times: user 731 ms, sys: 0 ns, total: 731 ms
#Wall time: 731 ms
And here is the Julia version, one loop, and one vectorized
# in the REPL
henon(x;a=1.4,b=0.3) = [1 - a*x[1]^2 - x[2], b*x[1]]
vectorized_henon(x;a=1.4,b=0.3) = cat(1 .- a.*x[:,1].^2 .- x[:,2], b.*x[:,1], dims=2)
iter(f, x, k) = (for _=1:k x=f(x) end; x)
# 10 000 dummy points, an array of arrays, and as a 2d array
y_vec = [ones(2)*0.34 for _=1:10000]
y_mat = ones(10000,2)*0.34
# for compilation
iter.(henon, y_vec, 10000)
iter(vectorized_henon, y_mat, 10000)
@time iter.(henon, y_vec, 10000)
# 5.148718 seconds (100.00 M allocations: 8.941 GiB, 30.70% gc time)
@time iter(vectorized_henon, y_mat, 10000)
# 1.236298 seconds (410.00 k allocations: 5.231 GiB, 12.20% gc time)
I’m sure I’m missing something basic here, but please tell me how to make this code more efficient.
Best wishes
Johannes