Performance measurement of floating number division vs multiplication

I want to know how expensive the floating number division is, compared to the multiplication. Using BenchmarkTools, I found they are the same.

julia> @benchmark $(Ref(a))[] * $(Ref(b))[]
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max):  1.372 ns … 15.229 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     1.546 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   1.556 ns ±  0.315 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

                                    ▇         █               
  ▃▃▂▁▁▁▁▂▄▃▁▁▁▁▁▂█▆▂▂▁▁▁▁▃█▃▂▁▁▁▁▁▃██▂▁▁▁▁▁▁██▆▃▂▂▂▂▁▁▂▄▄▄▃ ▃
  1.37 ns        Histogram: frequency by time        1.66 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark $(Ref(a))[] / $(Ref(b))[]
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max):  1.413 ns … 32.495 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     1.545 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   1.550 ns ±  0.470 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

            ▅▃         ▆          ▂▂         ▆█               
  ▃▃▂▁▁▁▁▁▁▄██▄▂▂▂▁▁▁▂▇██▃▂▁▁▁▁▁▂▅██▃▁▂▁▁▁▁▁▄███▄▂▂▂▁▁▁▁▂▂▂▂ ▃
  1.41 ns        Histogram: frequency by time        1.65 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

where a and b are Float64.

Given that the division should be several times slower than the multiplication, how do I interpret this result?

Benchmarking at the ns scale is very tricky. The following benchmark shows muhc more of what you would expect

x=rand(100)
julia> @benchmark foldl(*, $x)
BenchmarkTools.Trial: 10000 samples with 986 evaluations.
 Range (min … max):  49.161 ns … 112.373 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     49.452 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   51.039 ns ±   3.411 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ██        ▂▅      ▄▄         ▁▃                              ▁
  ██▃▄▅▆▆▇▇▆███▇▆█▇▇███▇██▇▇▇▇▅██▇▆▆▅▅▅▆▆▅▅▆▅▅▅▅▅▄▄▄▄▅▄▄▄▅▄▄▃▅ █
  49.2 ns       Histogram: log(frequency) by time      64.5 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark foldl(/, $x)
BenchmarkTools.Trial: 10000 samples with 792 evaluations.
 Range (min … max):  154.717 ns … 295.852 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     157.114 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   160.321 ns ±   8.681 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  █▄▄▄▄▅▆     ▁     ▁      ▁▅▁▁▂▂▂▃                             ▁
  ████████▇██████▇█████████████████▇█▇▇▆▇▇▇▇▆▆▅▆▇▅▅▅▅▅▄▄▅▆▅▅▄▃▅ █
  155 ns        Histogram: log(frequency) by time        189 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.
2 Likes

That looks more reasonable. Is the variable x an array?

Oops. I forgot. Yeah x=rand(100)

1 Like