# @btime's wrong output

**URL:** <https://discourse.julialang.org/t/btimes-wrong-output/27914>\
**Category:** New to Julia\
**Created:** [August 24, 2019, 12:00am UTC](https://discourse.julialang.org/t/btimes-wrong-output/27914 "2019-08-24T00:00:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![suiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suiato/32/1713_2.png) [@suiato](https://discourse.julialang.org/u/suiato)\
**Post date:** [August 24, 2019, 12:00am UTC](https://discourse.julialang.org/t/btimes-wrong-output/27914/1 "2019-08-24T00:00:42Z")

</div>

What’s wrong with the following code? (Julia 1.0.4)

> julia\> using BenchmarkTools
> 
> julia\> a = [1,2,3,4,5]  
> 5-element Array{Int64,1}:  
> 1  
> 2  
> 3  
> 4  
> 5
> 
> julia\> @btime $a .= $a.^2  
> 9.499 ns (0 allocations: 0 bytes)  
> 5-element Array{Int64,1}:  
> 1  
> 0  
> 1  
> 0  
> 1

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 24, 2019, 12:20am UTC](https://discourse.julialang.org/t/btimes-wrong-output/27914/2 "2019-08-24T00:20:54Z")

</div>

It evaluates your expression many times. That is the result of evaluating that expression a lot of times.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [August 24, 2019, 12:43am UTC](https://discourse.julialang.org/t/btimes-wrong-output/27914/3 "2019-08-24T00:43:00Z")

</div>

For some more details, this happens because of lots of int overflow.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [August 24, 2019, 1:13am UTC](https://discourse.julialang.org/t/btimes-wrong-output/27914/4 "2019-08-24T01:13:05Z")

</div>

This is quite beautiful, actually. It converged to the two fixed points 🙂.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [August 24, 2019, 1:18am UTC](https://discourse.julialang.org/t/btimes-wrong-output/27914/5 "2019-08-24T01:18:03Z")

</div>

Try with `Float64` and the ideas of @Oscar_Smith and @StefanKarpinski become clearer.

```julia
julia> a = [1.0,2,3,4,5]
5-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0
 5.0

julia> @btime $a .= $a.^2
  8.724 ns (0 allocations: 0 bytes)
5-element Array{Float64,1}:
   1.0
 Inf
 Inf
 Inf
 Inf

```

But when you remove the in-place dot, you get what you expected:

```julia
julia> a = [1,2,3,4,5]
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

julia> @btime $a = $a.^2
  245.757 ns (2 allocations: 160 bytes)
5-element Array{Int64,1}:
  1
  4
  9
 16
 25

```

---

<div class="post-metadata">

**Author:** ![suiato](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suiato/32/1713_2.png) [@suiato](https://discourse.julialang.org/u/suiato)\
**Post date:** [August 24, 2019, 1:34am UTC](https://discourse.julialang.org/t/btimes-wrong-output/27914/6 "2019-08-24T01:34:57Z")

</div>

Thank you all. Enjoyed your interesting answers 🙂
