# Using Benchmarktools with in-place functions and a default mutable struct

**URL:** <https://discourse.julialang.org/t/using-benchmarktools-with-in-place-functions-and-a-default-mutable-struct/103545>\
**Category:** General Usage\
**Tags:** benchmarktools\
**Created:** [September 5, 2023, 3:10pm UTC](https://discourse.julialang.org/t/using-benchmarktools-with-in-place-functions-and-a-default-mutable-struct/103545 "2023-09-05T15:10:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arnerob](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arnerob/32/49522_2.png) [@arnerob](https://discourse.julialang.org/u/arnerob)\
**Post date:** [September 5, 2023, 3:10pm UTC](https://discourse.julialang.org/t/using-benchmarktools-with-in-place-functions-and-a-default-mutable-struct/103545/1 "2023-09-05T15:10:09Z")

</div>

I want to benchmark a function by every time using the default value of a mutable struct. I tried to recreate a minimal working example of my code and what goes wrong:

```julia
mutable struct myNum{T<:Real} 
     n::T; 
     myNum()=new{Real}(2) 
end

function addOne!(myNum::p) 
    p.n=p.n+1 
    println(p) 
end

@benchmark addOne!(p) setup = (p=myNum())

```

This produces an output similar to

```julia
myNum{Real}(3)
myNum{Real}(3)
myNum{Real}(3)
myNum{Real}(3)
myNum{Real}(4)
myNum{Real}(3)
myNum{Real}(4)
myNum{Real}(5)
myNum{Real}(3)
myNum{Real}(4)
myNum{Real}(5)
myNum{Real}(6)
myNum{Real}(3)
myNum{Real}(4)
myNum{Real}(5)
myNum{Real}(6)
myNum{Real}(7)
myNum{Real}(3)
myNum{Real}(4)
myNum{Real}(5)
myNum{Real}(6)
myNum{Real}(7)
myNum{Real}(8)
myNum{Real}(3)
myNum{Real}(4)
myNum{Real}(5)
myNum{Real}(6)
myNum{Real}(7)
myNum{Real}(8)
myNum{Real}(9)
myNum{Real}(3)
myNum{Real}(4)
myNum{Real}(5)
myNum{Real}(6)
myNum{Real}(7)
myNum{Real}(8)
myNum{Real}(9)
myNum{Real}(10)
myNum{Real}(3)
myNum{Real}(4)
myNum{Real}(5)
myNum{Real}(6)
myNum{Real}(7)
myNum{Real}(8)
myNum{Real}(9)
myNum{Real}(10)
etc.

```

while I would want myNum{Real}(3) every time. I tried other things like `@benchmark addOne!(p) setup = (p=$myNum())` and `q=myNum(); @benchmark addOne!(p) setup = (p=$q)` but this doesn’t seem to fix anything. I wonder why this is and how to fix it?

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [September 5, 2023, 3:24pm UTC](https://discourse.julialang.org/t/using-benchmarktools-with-in-place-functions-and-a-default-mutable-struct/103545/2 "2023-09-05T15:24:28Z")

</div>

```julia
@benchmark addOne!(p) setup = (p=myNum()) evals=1

```

Should do it, but also see [`tune!` on benchmarkable with evals set. · Issue #328 · JuliaCI/BenchmarkTools.jl · GitHub](https://github.com/JuliaCI/BenchmarkTools.jl/issues/328)

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [September 5, 2023, 7:11pm UTC](https://discourse.julialang.org/t/using-benchmarktools-with-in-place-functions-and-a-default-mutable-struct/103545/3 "2023-09-05T19:11:10Z")

</div>

Funny enough I asked about a similar issue on Slack the other day. It was pointed out to me by @Sukera that `@benchmark` can be thought of as something like

```julia
for _ in 1:nsamples
    # setup runs here
    for _ in 1:nevals
         # the benchmarking expression runs here
    end
end

```

So multiple calls to your code can happen between each call to the setup code you provide. The solution @vchuravy gave with `evals=1` ensures you only run the inner loop once per setup call.
