Trig functions very slow

You’re forgetting to interpolate the (non-constant) variable f:

julia> f = sin
sin (generic function with 12 methods)

julia> @btime sin(r) setup=(r=rand())
  5.538 ns (0 allocations: 0 bytes)
0.1495824053430901

julia> @btime f(r) setup=(r=rand())
  38.765 ns (2 allocations: 32 bytes)
0.22395138641869694

julia> @btime $f(r) setup=(r=rand())
  5.837 ns (0 allocations: 0 bytes)
0.7879221208633208
2 Likes