When does it make sense to use `Base.MultiplicativeInverses`

kunzaatko
Tue, 19 Aug 2025 14:28:09 +0000
Performance

When does it make sense to use the Base.MultiplicativeInverses? I am considering storing the MultiplicativeInverse in my AbstractArray subtype which uses some index calculations based on some fields and the parent array. I thought that storing the MultiplicativeInverse of the numbers that I often divide by in my subtype would lead to a significant performance boost, but when I ran a quick benchmark I do not see any difference. Hence the question… When is it useful? What are some rules of thumb that I can abide by?

julia> un = rand(UInt64, 10_000);

julia> @btime div.($un, 56);
  6.620 μs (3 allocations: 78.20 KiB)

julia> using Base.MultiplicativeInverses

julia> @btime div.($un, $(MultiplicativeInverses.multiplicativeinverse(MultiplicativeInverses.unsigned(56))));
  6.530 μs (3 allocations: 78.20 KiB)

7 posts - 4 participants

Read full topic