Slow complex exponential evaluation

For reference, here’s how I would do it:

function imexp{T}(x::AbstractArray{T})
  out = similar(x,Complex{T})
  imexp!(out,x)
  out
end
function imexp!(out,x::AbstractArray)
  for i in eachindex(x)
    out[i]=cos(x[i]*2pi)+sin(x[i]*2pi)*im
  end
end
tt=collect(Float64,0:2^24-1)
@time imexp(tt);
@time imexp(tt);

I get:

5.993910 seconds (6 allocations: 256.000 MB, 1.38% gc time)
5.971867 seconds (6 allocations: 256.000 MB, 1.15% gc time)

If you want threads, just do

function imexp!(out,x::AbstractArray)
  Theads.@threads for i in eachindex(x)
    out[i]=cos(x[i]*2.0*pi)+sin(x[i]*2.0*pi)*im
  end
end

and make sure you enabled multithreading:

https://docs.julialang.org/en/stable/manual/parallel-computing/#setup

Edit

I realized that the Array constructor usage you did there actually works, so there’s no real timing difference. So it must all be multithreading.

1 Like