# @btime alters subsequent @time result

**URL:** <https://discourse.julialang.org/t/btime-alters-subsequent-time-result/111384>\
**Category:** Performance\
**Created:** [March 8, 2024, 10:46pm UTC](https://discourse.julialang.org/t/btime-alters-subsequent-time-result/111384 "2024-03-08T22:46:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chunjiw](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chunjiw](https://discourse.julialang.org/u/chunjiw)\
**Post date:** [March 8, 2024, 10:46pm UTC](https://discourse.julialang.org/t/btime-alters-subsequent-time-result/111384/1 "2024-03-08T22:46:14Z")

</div>

I’m trying to benchmark a struct implementation, where I encounter this. Minimal example:

```julia
using BenchmarkTools

struct S
end
(s::S)() = 1

let
    s = S()
    # @btime $s()
    @time s()
end

```

From which I get output `0.000000 seconds`. No surprise here since the function should not have any allocations. But things change if I add `@btime` before `@time` like so:

```julia
let
    s = S()
    @btime $s()
    @time s()
end

```

From which I get output

```julia
  1.266 ns (0 allocations: 0 bytes)
  0.000006 seconds (8 allocations: 384 bytes)

```

So `@time` depends on previous seemingly unrelated calls.

I tried @allocated and it correctly prints out `0`:

```julia
let
    s = S()
    @btime $s()
    println(@allocated s())
end

```

Does this mean there is a bug in `@time`? Did I use it wrong? Should I stop trusting it for tracking allocations? Thanks!

---

<div class="post-metadata">

**Author:** ![chunjiw](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chunjiw](https://discourse.julialang.org/u/chunjiw)\
**Post date:** [March 8, 2024, 10:48pm UTC](https://discourse.julialang.org/t/btime-alters-subsequent-time-result/111384/2 "2024-03-08T22:48:14Z")

</div>

`versioninfo()`:

```julia
julia> versioninfo()
Julia Version 1.10.2
Commit bd47eca2c8a (2024-03-01 10:14 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 12 × Intel(R) Core(TM) i7-6850K CPU @ 3.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, broadwell)
Threads: 1 default, 0 interactive, 1 GC (on 12 virtual cores)

```

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [March 9, 2024, 5:54am UTC](https://discourse.julialang.org/t/btime-alters-subsequent-time-result/111384/3 "2024-03-09T05:54:16Z")

</div>

I get the same behaviour on my system but it goes away if the code is in a function

```julia
using BenchmarkTools

struct S
end
(s::S)() = 1

function test(withbtime)
    s = S()
    withbtime && (@btime $s())
    @time s()
    return
end

test(true)
test(false)

```

I know that things behave slightly differently when run at global scope in the REPL so I suspect this is one of those interactions. The general advice is to always put your code in a function, especially when benchmarking and I guess this is one of the reasons why.

---

<div class="post-metadata">

**Author:** ![chunjiw](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chunjiw](https://discourse.julialang.org/u/chunjiw)\
**Post date:** [March 11, 2024, 7:17pm UTC](https://discourse.julialang.org/t/btime-alters-subsequent-time-result/111384/4 "2024-03-11T19:17:30Z")

</div>

According to the [doc](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#man-scope-table), `let` block is local scope as in a function.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 11, 2024, 8:15pm UTC](https://discourse.julialang.org/t/btime-alters-subsequent-time-result/111384/5 "2024-03-11T20:15:04Z")

</div>

I’m not sure where the funny business is coming from, but there’s definitely something funny here.

```julia-repl
julia> begin
       @benchmark nothing
       @time 1
       @time 1
       @time 1
       end
  0.000004 seconds (8 allocations: 384 bytes)
  0.000001 seconds (7 allocations: 368 bytes)
  0.000004 seconds (8 allocations: 384 bytes)
1

```

---

<div class="post-metadata">

**Author:** ![chunjiw](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chunjiw](https://discourse.julialang.org/u/chunjiw)\
**Post date:** [March 11, 2024, 8:16pm UTC](https://discourse.julialang.org/t/btime-alters-subsequent-time-result/111384/6 "2024-03-11T20:16:57Z")

</div>

Wow, thank you, this is a much better minimal example.

---

<div class="post-metadata">

**Author:** ![chunjiw](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chunjiw](https://discourse.julialang.org/u/chunjiw)\
**Post date:** [March 11, 2024, 8:18pm UTC](https://discourse.julialang.org/t/btime-alters-subsequent-time-result/111384/7 "2024-03-11T20:18:31Z")

</div>

I think this is a bug, so I opened an issue on GitHub [`@btime` alters subsequent `@time` result · Issue #53694 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/53694)  
Maybe we can discuss over there. Thanks everyone!
