@time and @btime show different allocations

Hey Julianners,

What can be the difference, that makes @time and @btime time different time in these scenarios?

a, b = 6, 7
foo(a, b) = begin
  for i = 1:2
    a, b=a + 6, b + 7+i
  end
end
@time foo(a,b)
@time foo(a,b)
@btime $foo($a,$b)

It gives:

  0.008822 seconds (17.17 k allocations: 1.149 MiB, 99.61% compilation time)
  0.000004 seconds (1 allocation: 32 bytes)
  1.555 ns (0 allocations: 0 bytes)

I know there is an allocation for the tuple return a,b… but why doesn’t @btime shows it? Or is it elliminated in some optimisation process?

I used @btime in one of my project that showed I eliminated one allocation this way in a speed intensive unit and not sure if it is just a mistake of the timing or there are still allocations?

Thank you for the answer!

@btime throws the code you wrote in a function and ignores compilation time. There’s no allocation for the tuple except when you assign it to a global (which is what the REPL does and what @time is showing), assuming your function actually has a return (a,b) in it.

1 Like

Why does the @btime throw the code in this scenario? That is really not what I wanted to measure. :smiley: