Fill SVector with uniformly sampled Float32 values

Tetrakai
Fri, 13 Sep 2024 20:54:04 +0000
Performance

There are quite a few topics on this, eg: Performance of generating uniform Float32 numbers

Given the current default behavior hasn’t been fixed, I tried a few things.

As a baseline, these all return Float64:
Edit: Bad benchmarks with length 2 (click for more details)
The SVector approaches are much faster. Then the code at the above link was modified thus:
Struct/Func Definitions (click for more details)
These return Float32:

julia> @btime SVector{16, Float32}(Float32.(rand(Uniform{Float32}(-.003, .003), 16)));
  169.113 ns (2 allocations: 320 bytes)

julia> @btime generate_uniform($rf);
  26.129 ns (0 allocations: 0 bytes)

julia> @btime generate_uniform2();
  26.244 ns (0 allocations: 0 bytes)

1. Is there any way faster than generate_uniform($rf)?
– Presumably ~4x is possible given the Float64 benchmarks

2. I was surprised hard-coding the uniform distribution parameters was (slightly but consistently) slower than passing a struct, ie generate_uniform vs generate_uniform2. Is there a general reason for that?

Thanks.

Edit
Correct Float64 benchmarks (all of length 16):

julia> @btime rand(Uniform(-.003, .003), 16);
  131.719 ns (1 allocation: 192 bytes)

julia> @btime rand(Uniform{Float64}(-.003, .003), 16);
  132.685 ns (1 allocation: 192 bytes)

julia> @btime rand(Uniform{Float32}(-.003, .003), 16);
  131.877 ns (1 allocation: 192 bytes)

julia> @btime @SVector rand(Uniform(-.003, .003), 16);
  36.240 ns (0 allocations: 0 bytes)

julia> @btime @SVector rand(Uniform{Float64}(-.003, .003), 16);
  36.247 ns (0 allocations: 0 bytes)

julia> @btime @SVector rand(Uniform{Float32}(-.003, .003), 16);
  36.229 ns (0 allocations: 0 bytes)

6 posts - 3 participants