# Running small functions bundled in an outer function taking twice the time of running them separately

**URL:** <https://discourse.julialang.org/t/running-small-functions-bundled-in-an-outer-function-taking-twice-the-time-of-running-them-separately/36894>\
**Category:** Performance\
**Tags:** benchmarktools\
**Created:** [April 2, 2020, 9:02am UTC](https://discourse.julialang.org/t/running-small-functions-bundled-in-an-outer-function-taking-twice-the-time-of-running-them-separately/36894 "2020-04-02T09:02:07Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 2, 2020, 9:12am UTC](https://discourse.julialang.org/t/running-small-functions-bundled-in-an-outer-function-taking-twice-the-time-of-running-them-separately/36894/2 "2020-04-02T09:12:57Z")

</div>

I suspect it has to do with some of the data fitting into a CPU cache with the individual benchmarks, but only a higher level cache for the combined ones. I can replicate the phenomenon on 1.4, with multiple runs giving somewhat inconsistent timings.

In any case, making `x` and `v` 10x larger resolves the inconsistency for me (you may have to increase it more if you have a recent desktop CPU, mine is a puny laptop CPU with little cache) eg

```julia
julia> @btime toPolar!($x)
  4.520 ms (0 allocations: 0 bytes)

julia> @btime toCartesian!($x)
  1.284 ms (0 allocations: 0 bytes)

julia> @btime move!($x, $v, $T)
  148.501 μs (0 allocations: 0 bytes)

julia> @btime outerFunction!($x, $v, $T)
  5.503 ms (0 allocations: 0 bytes)

```

what the relevant benchmark is depends on your data size I guess.

Also, cf

> [@PSA: Microbenchmarks remember branch history](https://discourse.julialang.org/t/psa-microbenchmarks-remember-branch-history/17436):
>
> Something to keep in mind when running microbenchmarks: The sneaky CPU can remember astoundingly long branch history patterns. If you run @btime on somthing, then you generate a periodic branching pattern: The period length is the size of your benchmark, and it is repeated very often by the benchmark loop. This is because @btime runs your code multiple times and then averages the timings, because we don’t get clock with cycle-precision. This can spoil benchmarks and has affected some BaseBenchm…

---

_[View the full topic](https://discourse.julialang.org/t/running-small-functions-bundled-in-an-outer-function-taking-twice-the-time-of-running-them-separately/36894)._
