Found a behaviour of randn that I didnβt really understand. Since there exists a version where you can select the type I assume that that would be more efficient than generating and then converting, but it seems that this depends on the size of the generated data.
Here we see f1 using the conversion and f2 using the direct call. f1 is clearly slower when generating only 10 numbers, but a little faster when generating 2000 numbers. Anyone have a clue as to why this is?
julia> using BenchmarkTools
julia> f1(dims) = Float32.(randn(dims))
f1 (generic function with 2 methods)
julia> f2(dims) = randn(Float32, dims)
f2 (generic function with 2 methods)
julia> @benchmark f1(10)
BenchmarkTools.Trial: 10000 samples with 919 evaluations.
Range (min β¦ max): 107.929 ns β¦ 1.170 ΞΌs β GC (min β¦ max): 0.00% β¦ 84.78%
Time (median): 118.046 ns β GC (median): 0.00%
Time (mean Β± Ο): 124.795 ns Β± 55.602 ns β GC (mean Β± Ο): 3.12% Β± 6.36%
ββββββ
ββ β β
βββββββββββββββββ
β
ββββββββββββββββββββββββββββββ
β
ββ
β
ββ
ββββββ β
108 ns Histogram: log(frequency) by time 281 ns <
Memory estimate: 288 bytes, allocs estimate: 2.
julia> @benchmark f2(10)
BenchmarkTools.Trial: 10000 samples with 968 evaluations.
Range (min β¦ max): 71.681 ns β¦ 770.913 ns β GC (min β¦ max): 0.00% β¦ 88.68%
Time (median): 78.347 ns β GC (median): 0.00%
Time (mean Β± Ο): 83.177 ns Β± 34.070 ns β GC (mean Β± Ο): 1.95% Β± 4.54%
ββββββ
ββββ β
βββββββββββββββββ
ββ
ββββββ
ββββββββββββββββββββββββββββββββ
βββ β
71.7 ns Histogram: log(frequency) by time 161 ns <
Memory estimate: 128 bytes, allocs estimate: 1.
julia> @benchmark f1(2000)
BenchmarkTools.Trial: 10000 samples with 4 evaluations.
Range (min β¦ max): 6.650 ΞΌs β¦ 233.773 ΞΌs β GC (min β¦ max): 0.00% β¦ 80.82%
Time (median): 7.554 ΞΌs β GC (median): 0.00%
Time (mean Β± Ο): 8.138 ΞΌs Β± 8.375 ΞΌs β GC (mean Β± Ο): 4.01% Β± 3.77%
ββββββββββ
βββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββ β
6.65 ΞΌs Histogram: frequency by time 10.6 ΞΌs <
Memory estimate: 23.69 KiB, allocs estimate: 2.
julia> @benchmark f2(2000)
BenchmarkTools.Trial: 10000 samples with 4 evaluations.
Range (min β¦ max): 7.191 ΞΌs β¦ 276.534 ΞΌs β GC (min β¦ max): 0.00% β¦ 86.09%
Time (median): 7.627 ΞΌs β GC (median): 0.00%
Time (mean Β± Ο): 8.019 ΞΌs Β± 6.315 ΞΌs β GC (mean Β± Ο): 1.70% Β± 2.12%
ββ
ββββββββ
ββββββββββββββ β β
βββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββ β
7.19 ΞΌs Histogram: log(frequency) by time 10.8 ΞΌs <
Memory estimate: 7.94 KiB, allocs estimate: 1.