Vector{Float} times Vector{Complex} slower than Vector{Complex} times Vector{Complex}

If you define
Q = collect(-10:.01:10) v = Q.-transpose(Q) cv = complex.(v) comp = exp.(-im.*Q)
then multiplying cvcomp takes 2.94ms on my computer compared with 3.41ms for vcomp (times measured with btime) Any idea why this is?

The former has the extra step of converting Floats to Complexs.

That would make sense if it was also slower for single element multiplication, but it’s not. Also it ideally would be possible to perform the multiplication without conversion since multiplying real by complex is easy

First of all, running your code, I get

Q = collect(-10:.01:10)
v = Q.-transpose(Q)
cv = complex.(v)
comp = exp.(-im.*Q)

using BenchmarkTools
@btime $cv * $comp; # 2.561 ms (2 allocations: 31.39 KiB)
@btime $v * $comp; # 2.610 ms (2 allocations: 31.39 KiB)

Secondly, cv and v are matrices, not vectors.
( typeof(v) == Array{Complex{Float64},2}).

Also note that you generally doesn’t have to (and shouldn’t) collect ranges.

1 Like

Could it be a blas difference? I’m on linux.

https://github.com/JuliaLang/julia/issues/20060

1 Like

Thanks for finding the issue. It would be great if this were improved, though I understand it’s probably difficult if BLAS doesn’t support it natively