# @btime does not work properly

**URL:** <https://discourse.julialang.org/t/btime-does-not-work-properly/107825>\
**Category:** New to Julia\
**Created:** [December 19, 2023, 4:45pm UTC](https://discourse.julialang.org/t/btime-does-not-work-properly/107825 "2023-12-19T16:45:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsoler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vsoler/32/30667_2.png) [@vsoler](https://discourse.julialang.org/u/vsoler)\
**Post date:** [December 19, 2023, 4:45pm UTC](https://discourse.julialang.org/t/btime-does-not-work-properly/107825/1 "2023-12-19T16:45:08Z")

</div>

I am astonished by how the @btime (BenchmarkTools) macro works (or misworks).

I have the following simple program, which read quotes from the S&P500 index and from Microsoft.

```julia
using MarketData, DataFrames, BenchmarkTools

df = DataFrame(Date = Date[])
tickers = ["^GSPC", "MSFT"]

function read_data(df, tickers)
    println("Entering read_data...")
    for ticker in tickers
        df = outerjoin(df, select(DataFrame(yahoo(ticker)), :timestamp => :Date, :AdjClose => ticker), on = :Date)
    end 
    
    df = dropmissing(df)
    sort!(df, :Date)
    println("Exiting read_data...")
    return df
end

function main1(df, tickers)
    println("Entering main1...")
    df = read_data(df, tickers)
    println(first(df, 5))
    println("Exiting main1...\n")
    return
end

@btime main1(df, tickers)

```

If you run it, you will see that read\_data is executed 8 times, while it supposed to execute only one.

If, alternatively, I use @time instead of @btime, it executes only one time, which is correct.

![image](https://global.discourse-cdn.com/julialang/original/3X/d/0/d085b13897861af45626ef7e81905c5e9f04a8d5.png)

![image](https://global.discourse-cdn.com/julialang/original/3X/7/e/7ec98d071f3d94d873d0adc85a3366e857ef250e.png)

I have not found any explanation to this misbehavior. I’m using ver 1.9.2 of Julia

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [December 19, 2023, 4:54pm UTC](https://discourse.julialang.org/t/btime-does-not-work-properly/107825/2 "2023-12-19T16:54:17Z")

</div>

The benchmark tools typically run your code several times and produce statistics on the timings. Best starting place for documentation is the built-in help system. E.g. type `? @btime` at the Julia prompt.

The `@btime` help only hints at the multiple runs, but it points to `@benchmark`, which gives you the full explanation.

```julia
help?> @btime
  @btime expression [other parameters...]

  Similar to the @time macro included with Julia, this executes an expression, printing the time it took to execute and
  the memory allocated before returning the value of the expression.

  Unlike @time, it uses the @benchmark macro, and accepts all of the same additional parameters as @benchmark. The
  printed time is the minimum elapsed time measured during the benchmark.

help?> @benchmark
  @benchmark <expr to benchmark> [setup=<setup expr>]

  Run benchmark on a given expression.

  Example
  ≡≡≡≡≡≡≡≡≡

  The simplest usage of this macro is to put it in front of what you want to benchmark.

  julia> @benchmark sin(1)
  BenchmarkTools.Trial:
    memory estimate: 0 bytes
    allocs estimate: 0
    --------------
    minimum time: 13.610 ns (0.00% GC)
    median time: 13.622 ns (0.00% GC)
    mean time: 13.638 ns (0.00% GC)
    maximum time: 21.084 ns (0.00% GC)
    --------------
    samples: 10000
    evals/sample: 998

```

(output truncated)

---

<div class="post-metadata">

**Author:** ![vsoler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vsoler/32/30667_2.png) [@vsoler](https://discourse.julialang.org/u/vsoler)\
**Post date:** [December 19, 2023, 4:55pm UTC](https://discourse.julialang.org/t/btime-does-not-work-properly/107825/3 "2023-12-19T16:55:52Z")

</div>

Understood !

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [December 19, 2023, 5:39pm UTC](https://discourse.julialang.org/t/btime-does-not-work-properly/107825/4 "2023-12-19T17:39:24Z")

</div>

There are a few handy keywords as well. If you want to force something to run only once you can always do

```julia
using BenchmarkTools
@benchmark sin(1) samples=1 evals=1

```

Or sometimes I’ll have a longer function to test where there not enough trials, so you can set it up to run as many times as it can for a set period of time

```julia
@benchmark my_long_running_function($x) seconds=30

```

I will use both of those examples quite frequently.
