BenchmarkTools.jl icon indicating copy to clipboard operation
BenchmarkTools.jl copied to clipboard

@btime not printing as expected in a loop

Open Luapulu opened this issue 4 years ago • 3 comments

When I'm comparing different versions of a function I like to do something like

function func1(x)
    # code
end

function func2(x)
    # code
end

function func3(x)
    # code
end

for i in 1:3
    print(i, ": ")
    @btime $(Symbol(:func, i))($(12345))
end

I'd expect this to print something like

1:   19.682 μs (914 allocations: 42.84 KiB)
2:   21.141 μs (914 allocations: 42.84 KiB)
3:   21.889 μs (914 allocations: 42.84 KiB)

but I get

19.682 μs (914 allocations: 42.84 KiB)
2:   21.141 μs (914 allocations: 42.84 KiB)
3:   21.889 μs (914 allocations: 42.84 KiB)

What's going on?

Luapulu avatar Dec 23 '20 10:12 Luapulu

I'm having the same issue and it would be great to get some insight on this.

calebwin avatar Feb 10 '21 08:02 calebwin

Works fine for me:

julia> for i in 1:3
           print(i, ": ")
           @btime $(Symbol(:func, i))($(Ref(12345))[])
       end
1:   1.214 ns (0 allocations: 0 bytes)
2:   1.017 ns (0 allocations: 0 bytes)
3:   1.058 ns (0 allocations: 0 bytes)

KristofferC avatar Jul 28 '21 09:07 KristofferC

This looks like a common bug when running in Atom. Often when I print to the terminal, the first print is ignored. I therefore add a print("") if I am trying to align something.

bojeryd91 avatar Mar 13 '22 18:03 bojeryd91