henry2004y / TestParticle.jl

Test particle tracing in electromagnetic field
https://henry2004y.github.io/TestParticle.jl/dev/
MIT License
15 stars 3 forks source link

`hypot` slower than direct computation? #166

Closed henry2004y closed 5 months ago

henry2004y commented 5 months ago
julia> using BenchmarkTools

julia> foo(u) = √(u[1]^2 + u[2]^2 + u[3]^2)
foo (generic function with 1 method)

julia> foo2(u) = hypot(u...)
foo2 (generic function with 1 method)

julia> foo3(u) = hypot(u[1], u[2], u[3])
foo3 (generic function with 1 method)

julia> u = rand(3)
3-element Vector{Float64}:
 0.37237800050918357
 0.05596914175621526
 0.3852787920971138

julia> @benchmark foo($u)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max):  2.500 ns … 34.400 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     2.600 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   2.685 ns ±  1.206 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

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

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark foo2($u)
BenchmarkTools.Trial: 10000 samples with 976 evaluations.
 Range (min … max):  77.152 ns … 894.980 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     81.352 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   86.187 ns ±  18.366 ns  ┊ GC (mean ± σ):  0.31% ± 2.41%

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

 Memory estimate: 64 bytes, allocs estimate: 4.

julia> @benchmark foo3($u)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max):  4.400 ns … 44.500 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     4.600 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   4.888 ns ±  1.770 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

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

 Memory estimate: 0 bytes, allocs estimate: 0.
henry2004y commented 5 months ago

a9e184c