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

@btime alters subsequent @time result #53694

Open
chunjiw opened this issue Mar 11, 2024 · 2 comments
Open

@btime alters subsequent @time result #53694

chunjiw opened this issue Mar 11, 2024 · 2 comments

Comments

@chunjiw
Copy link

chunjiw commented Mar 11, 2024

Also posted in discource
Minimal example:

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:

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

From which I get output

  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:

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

If the code is in a function, then @time output is correct, as pointed out in discourse. But personally I think this is still a bug.

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)

Julia is installed via juliaup.

@chunjiw
Copy link
Author

chunjiw commented Mar 11, 2024

@Zentrik
Copy link
Contributor

Zentrik commented Mar 19, 2024

I suspect this is due to the compiler not optimising the code as well in the second case, e.g. replace @time with @timed and you will see less allocations (@time calls @timed internally). Given wrapping the code in a function makes the allocation disappear I'm not sure how to look at the compiled code and verify.

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

2 participants