Wildly varying timings for round and round

On Julia v0.6, I @time and @benchmark round(...) and its dot counterpart, round.(…)``. The macro @time reports 0.6 ms for round and 29 ms for the dot version; the macro @benchmark shows that round.(...) is 3 orders of magnitude faster than reported by @time and 20 times faster than round(...). (I understand that round is deprecated in v0.6.)

What is the reason for the wild variation?

julia> a = randn(100);

julia> @time round(a, 3, 2);
  0.000624 seconds (88 allocations: 12.656 KiB)

julia> @time round.(a, 3, 2);
  0.029180 seconds (3.98 k allocations: 226.429 KiB)

julia> @benchmark round(a, 3, 2)
BenchmarkTools.Trial:
  memory estimate:  12.50 KiB
  allocs estimate:  84
  --------------
  minimum time:     236.047 µs (0.00% GC)
  median time:      251.742 µs (0.00% GC)
  mean time:        261.373 µs (0.46% GC)
  maximum time:     2.891 ms (83.55% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> @benchmark round.(a, 3, 2)
BenchmarkTools.Trial:
  memory estimate:  1.89 KiB
  allocs estimate:  24
  --------------
  minimum time:     12.376 µs (0.00% GC)
  median time:      13.885 µs (0.00% GC)
  mean time:        14.815 µs (1.58% GC)
  maximum time:     2.424 ms (96.41% GC)
  --------------
  samples:          10000
  evals/sample:     1

Deprecated functions are slow because they do a stacktrace lookup to see if the deprecation warning has already been printed.

1 Like

You should also interpolate variables into the benchmark macros:

julia> @btime round.(a, 3, 2);
  15.034 μs (24 allocations: 1.89 KiB)

julia> @btime round.($a, 3, 2);
  6.683 μs (1 allocation: 896 bytes)
2 Likes

I have been told that already. My apologies.