Slow performance for `rand(ComplexF64, ...)`

I was quite surprised to see this performance difference:

julia> using BenchmarkTools

julia> N = 1000;

julia> @btime complex.(rand($N, $N), rand($N, $N));
  4.852 ms (6 allocations: 30.52 MiB)

julia> @btime rand(ComplexF64, $N, $N);
  11.201 ms (2 allocations: 15.26 MiB)

It’s even worse for smaller N:

julia> N = 100;

julia> @btime complex.(rand($N, $N), rand($N, $N));
  20.001 μs (6 allocations: 312.73 KiB)

julia> @btime rand(ComplexF64, $N, $N);
  99.300 μs (2 allocations: 156.33 KiB)

Is this a known performance issue? I would have expected rand(ComplexF64, ...) to be the fastest of the two.

I’m on v1.5.3.

1 Like

This seems fixed on master. (I can confirm on 1.5.3)

The first call to this also really slow. Is it the same issue?

julia> using StaticArrays

julia> rand(SVector{10000,Float64})

No. That’s just compilation time from using a 10000 length SVector you shouldn’t be using StaticArrays for lengths of greater than 100 or so because the compiler has to make a new version for each different length.

3 Likes

I can confirm that this seems to be fixed on (7 day old) master:

julia> N = 1000;

julia> @btime complex.(rand($N, $N), rand($N, $N));
  4.040 ms (6 allocations: 30.52 MiB)

julia> @btime rand(ComplexF64, $N, $N);
  2.932 ms (2 allocations: 15.26 MiB)

julia> N = 100;

julia> @btime complex.(rand($N, $N), rand($N, $N));
  28.350 μs (6 allocations: 312.73 KiB)

julia> @btime rand(ComplexF64, $N, $N);
  30.222 μs (2 allocations: 156.33 KiB)

Yeah, it seems sort of fixed, though I thought maybe the advantage would be bigger, especially for N = 100.