# Allocations and time of running the same program twice are orders of magnitude larger than running them separately

**URL:** https://discourse.julialang.org/t/allocations-and-time-of-running-the-same-program-twice-are-orders-of-magnitude-larger-than-running-them-separately/114078
**Category:** New to Julia
**Tags:** performance, memory-allocation, benchmark
**Created:** [May 10, 2024, 2:42am UTC](https://discourse.julialang.org/t/allocations-and-time-of-running-the-same-program-twice-are-orders-of-magnitude-larger-than-running-them-separately/114078 "2024-05-10T02:42:00Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![dieg0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dieg0/32/210143_2.png) [@dieg0](https://discourse.julialang.org/u/dieg0)
#### Post date: [May 15, 2024, 3:17pm UTC](https://discourse.julialang.org/t/allocations-and-time-of-running-the-same-program-twice-are-orders-of-magnitude-larger-than-running-them-separately/114078/7 "2024-05-15T15:17:13Z")

</div>

Yes, this seems to be it. So `@benchmark` does not seem to be a good way to evaluate the performance of a fixed point algorithm like mine. Writing a wrapper function of the following form (passing the other parameters):

```julia
function evaluate(δ_in, Xβ, params)
    δ_out = copy(δ_in)
    invert_shares_δ!(δ_out, Xβ, params)
    return δ_out
end

```

Gives the “correct” benchmark performance:

```julia
BenchmarkTools.Trial: 151 samples with 1 evaluation.
 Range (min … max): 18.512 ms … 378.933 ms ┊ GC (min … max): 0.00% … 90.32%
 Time (median): 20.174 ms ┊ GC (median): 0.00%
 Time (mean ± σ): 34.223 ms ± 68.247 ms ┊ GC (mean ± σ): 39.90% ± 18.29%

  █▁                                                            
  ██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄▁▄▅ ▄
  18.5 ms Histogram: log(frequency) by time 372 ms <

 Memory estimate: 307.06 MiB, allocs estimate: 18968.

```

This is roughly half of the time than running the code twice together (see my benchmark estimates above). So it makes sense.

Sad news—my code is slower than I thought! I guess I will have to make it faster. 😅

---

_[View the full topic](https://discourse.julialang.org/t/allocations-and-time-of-running-the-same-program-twice-are-orders-of-magnitude-larger-than-running-them-separately/114078)._
