# Wrong results when I use @btime

**URL:** <https://discourse.julialang.org/t/wrong-results-when-i-use-btime/71324>\
**Category:** General Usage\
**Tags:** question, package, benchmarktools\
**Created:** [November 11, 2021, 10:29am UTC](https://discourse.julialang.org/t/wrong-results-when-i-use-btime/71324 "2021-11-11T10:29:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [November 11, 2021, 10:29am UTC](https://discourse.julialang.org/t/wrong-results-when-i-use-btime/71324/1 "2021-11-11T10:29:48Z")

</div>

Dear all,

I am quite baffled by the results I get when using `@btime`. I was following this simple lecture: [https://cuda.juliagpu.org/stable/tutorials/introduction/](https://cuda.juliagpu.org/stable/tutorials/introduction/) and just by copying and pasting from this page to a Julia script, got the following:

```julia
using Test                                                                       
using BenchmarkTools                                                             
                                                                                 
N = 2^20                                                                         
x = fill(1.0f0, N) # a vector filled with 1.0 (Float32)                         
y = fill(2.0f0, N) # a vector filled with 2.0                                   
                                                                                 
function sequential_add!(a, b)                                                   
  for i in eachindex(a, b)                                                       
    @inbounds a[i] += b[i]                                                       
  end                                                                            
  return nothing                                                                 
end                                                                              
                                                                                 
# This will pass the test                                                        
fill!(y, 2.0f0)                                                                  
sequential_add!(y, x)                                                            
@test all(y .== 3.0f0)                                                           
                                                                                 
# This will fail the test                                                        
fill!(y, 2.0f0)                                                                  
@btime sequential_add!($y, $x)                                                   
@test all(y .== 3.0f0)

```

As the comments I inserted above indicate, after the first call to `sequential_add!` I get correct results, but after the second which goes through `@btime` macro, the test doesn’t pass and I get rather strange results for `y`, it is filled with `37032.0` or `35204.0`, depending on the run.

There is no creativity from my side in the above example. Except for the two comments and the `@test` in the last line, all is copied and pasted from the link I give above.

Does anyone have an idea what is going on here? I am using Julia 1.6.3 and am launching it with `julia --check-bounds=no --threads 6`, although the command line options don’t change the outcome.

_Cheers_

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 11, 2021, 10:35am UTC](https://discourse.julialang.org/t/wrong-results-when-i-use-btime/71324/2 "2021-11-11T10:35:23Z")

</div>

[This thread](https://discourse.julialang.org/t/how-to-benchmark-in-place-functions/36493) might be of interest for benchmarking your in-place functions.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 11, 2021, 10:37am UTC](https://discourse.julialang.org/t/wrong-results-when-i-use-btime/71324/3 "2021-11-11T10:37:27Z")

</div>

In short: `@btime` runs the function several times and thus updates `y` several times.

---

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [November 11, 2021, 10:42am UTC](https://discourse.julialang.org/t/wrong-results-when-i-use-btime/71324/4 "2021-11-11T10:42:54Z")

</div>

Indeed, changing the last few lines to:

```julia
@btime begin                                                                     
  fill!(y, 2.0f0)                                                                
  sequential_add!($y, $x)                                                        
end

```

fixed the thing 🙂

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 11, 2021, 10:55am UTC](https://discourse.julialang.org/t/wrong-results-when-i-use-btime/71324/5 "2021-11-11T10:55:40Z")

</div>

Although, to do the testing properly you should look at the link posted by @rafael.guerra

---

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [November 11, 2021, 10:57am UTC](https://discourse.julialang.org/t/wrong-results-when-i-use-btime/71324/6 "2021-11-11T10:57:40Z")

</div>

I see it now, thanks a lot.
