# Performance measurement of floating number division vs multiplication

**URL:** https://discourse.julialang.org/t/performance-measurement-of-floating-number-division-vs-multiplication/92874
**Category:** Performance
**Created:** [January 12, 2023, 3:01pm UTC](https://discourse.julialang.org/t/performance-measurement-of-floating-number-division-vs-multiplication/92874 "2023-01-12T15:01:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)
#### Post date: [January 12, 2023, 3:01pm UTC](https://discourse.julialang.org/t/performance-measurement-of-floating-number-division-vs-multiplication/92874/1 "2023-01-12T15:01:10Z")

</div>

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

```julia
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?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [January 12, 2023, 3:04pm UTC](https://discourse.julialang.org/t/performance-measurement-of-floating-number-division-vs-multiplication/92874/2 "2023-01-12T15:04:01Z")

</div>

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

```julia
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.

```

---

<div class="post-metadata">

### Author: ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)
#### Post date: [January 12, 2023, 3:19pm UTC](https://discourse.julialang.org/t/performance-measurement-of-floating-number-division-vs-multiplication/92874/3 "2023-01-12T15:19:51Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [January 12, 2023, 3:30pm UTC](https://discourse.julialang.org/t/performance-measurement-of-floating-number-division-vs-multiplication/92874/4 "2023-01-12T15:30:55Z")

</div>

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