Well, this is already > 6x faster:
function f2()
M = Matrix{RGB{Float32}}(undef, 1080, 1980)
@inbounds for j in 1:1980
for i in 1:1080
M[i,j] = RGB(rand(), rand(), rand())
end
end
return M
end
julia> @btime f1(); # f1() = rand(RGB{Float32},1080,1980)
137.335 ms (3 allocations: 24.47 MiB)
julia> @btime f2();
20.709 ms (2 allocations: 24.47 MiB)
But of course, you should probably think about algorithmic improvements, i.e. how to utilise the fact that statistical randomness is not the primary objective. On the purely technical side, you might try other RNGs, for example from https://github.com/sunoru/RandomNumbers.jl.
Update:
using RandomNumbers
rng_xor = RandomNumbers.Xorshifts.Xoroshiro128Star()
function f2_rng(rng)
M = Matrix{RGB{Float32}}(undef, 1080, 1980)
@inbounds for j in 1:1980
for i in 1:1080
M[i,j] = RGB(rand(rng), rand(rng), rand(rng))
end
end
return M
end
I find
julia> @btime f2_rng($rng_xor);
8.903 ms (2 allocations: 24.47 MiB)