Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hypot slower than direct computation? #166

Closed
henry2004y opened this issue Apr 14, 2024 · 1 comment
Closed

hypot slower than direct computation? #166

henry2004y opened this issue Apr 14, 2024 · 1 comment

Comments

@henry2004y
Copy link
Owner

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
Copy link
Owner Author

a9e184c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant