In looking at some recent updates in Distributions I saw code like this
dot(w, inv.(x)) and thought that better could be done to avoid allocating
inv.(x) however what I came up with was always slower and I’m not sure why.
julia> x = randn(100_000) .+ 100 julia> y = randn(100_000) .+ 100 julia> f(x, y) = dot(x, inv.(y)) julia> g(x, y) = sum(@inbounds x[i]/y[i] for i in eachindex(x)) julia> @btime f($x, $y) 74.495 μs (2 allocations: 781.33 KiB) 100011.86751975697 julia> @btime g($x, $y) 111.424 μs (2 allocations: 64 bytes) 100011.86751975746
Any idea why the second one is slower? purely in terms of flops it should be about the same shouldn’t it?