Hello, just starting to make the switch to Julia from Matlab/C. I’ve been reading up on the speed advantages of Julia and recognize the power that can be wielded in the right hands. I was hoping I could receive some pointers on how to optimize my code. I’m comparing the code below to a vectorized C style that performs the same calculation but has been converted to julia’s syntax. The code below is both slower and requires significantly more memory allocation than the C version. I use the same variables declared outside of each function with most of the being Vector{Floats64} with b1 being a Vector{Complex{Float64}}.

Any recommendations on how I could speed this up and make it significantly less memory intensive would be greatly appreciated!

```
using LinearAlgebra
x_pts = 1000
num_pts = 5000
Xpos = range(-FOV/2,FOV/2,length = x_pts)
M = zeros(3,num_pts)
M[:,1] = [0;0;1]
function Sim1d(Xpos, num_pts, resol, b1, gradx, offset, M)
Mout = zeros(3, length(Xpos))
B1 = [0.0; 0.0; 0.0]
for n = 1:length(Xpos)
for tt = 1:num_pts-1
Δω = gam * Xpos[n] * grad[tt] + offset[n]
B1 = [real(b1[tt]); imag(b1[tt]); Δω]
θ = norm(B1) * resol
if abs(θ) > 1e-9
B1 = B1 / norm(B1)
M[:, tt+1] = M[:, tt] * cos(θ) + cross(B1, M[:, tt]) * sin(θ) +
B1 * dot(B1, M[:, tt]) * (1 - cos(θ))
else
M[:, tt+1] = M[:, tt]
end
end
Mout[:, n] = M[:, end]
end
return Mout
end
```