# Benchmarking a function with side effects

**URL:** https://discourse.julialang.org/t/benchmarking-a-function-with-side-effects/48531
**Category:** General Usage
**Tags:** benchmark, benchmarktools
**Created:** [October 17, 2020, 3:56am UTC](https://discourse.julialang.org/t/benchmarking-a-function-with-side-effects/48531 "2020-10-17T03:56:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [October 17, 2020, 3:56am UTC](https://discourse.julialang.org/t/benchmarking-a-function-with-side-effects/48531/1 "2020-10-17T03:56:41Z")

</div>

I want to compare `push!`ing an element to an `Array` with `push!!`ing an element to a `Tuple` using `BenchmarkTools.jl` and [`BangBang.jl`](https://juliafolds.github.io/BangBang.jl/dev/#BangBang.push!!):

```julia
julia> using BenchmarkTools, BangBang

julia> a = 1:20 |> collect;

julia> b = Tuple(a);

julia> @btime push!(a, 1)
  19.274 ns (0 allocations: 0 bytes)
10470522-element Array{Int64,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  ⋮
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1
  1

julia> @btime push!!(b, 1)
  26.583 ns (1 allocation: 176 bytes)
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1)

```

However, `push!` will change `a` within each loop of the benchmark while `push!!` will not change the `Tuple`. That forms unfair comparisons (even though in this case, `push!` seems to be much faster?). How can I benchmark pushing exactly 1 element into `a`?

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [October 17, 2020, 4:01am UTC](https://discourse.julialang.org/t/benchmarking-a-function-with-side-effects/48531/2 "2020-10-17T04:01:14Z")

</div>

Use `setup`

```julia
@benchmark push!(a,1) setup=(a=collect(1:20))

```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [October 17, 2020, 4:59am UTC](https://discourse.julialang.org/t/benchmarking-a-function-with-side-effects/48531/3 "2020-10-17T04:59:52Z")

</div>

Also `evals=1` to avoid repeatedly pushing to the same array (ref: [How to benchmark append!? - #7 by rdeits](https://discourse.julialang.org/t/how-to-benchmark-append/47272/7) )
