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

**URL:** <https://discourse.julialang.org/t/slow-performance-for-rand-complexf64/53032>\
**Category:** Performance\
**Created:** [January 8, 2021, 8:46am UTC](https://discourse.julialang.org/t/slow-performance-for-rand-complexf64/53032 "2021-01-08T08:46:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 8, 2021, 8:46am UTC](https://discourse.julialang.org/t/slow-performance-for-rand-complexf64/53032/1 "2021-01-08T08:46:08Z")

</div>

I was quite surprised to see this performance difference:

```julia
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
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.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 8, 2021, 2:59pm UTC](https://discourse.julialang.org/t/slow-performance-for-rand-complexf64/53032/2 "2021-01-08T14:59:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 8, 2021, 3:11pm UTC](https://discourse.julialang.org/t/slow-performance-for-rand-complexf64/53032/3 "2021-01-08T15:11:26Z")

</div>

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

```julia
julia> using StaticArrays

julia> rand(SVector{10000,Float64})

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 8, 2021, 3:14pm UTC](https://discourse.julialang.org/t/slow-performance-for-rand-complexf64/53032/4 "2021-01-08T15:14:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 8, 2021, 3:20pm UTC](https://discourse.julialang.org/t/slow-performance-for-rand-complexf64/53032/5 "2021-01-08T15:20:56Z")

</div>

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

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 8, 2021, 4:12pm UTC](https://discourse.julialang.org/t/slow-performance-for-rand-complexf64/53032/6 "2021-01-08T16:12:48Z")

</div>

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