# Making @benchmark outputs statistically meaningful, and actionable

**URL:** <https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256>\
**Category:** Profiling\
**Tags:** benchmark, benchmarktools\
**Created:** [September 26, 2023, 11:38am UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256 "2023-09-26T11:38:25Z")\
**Posts on this page:** 20\
**Page:** 5

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [September 28, 2023, 2:26pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/81 "2023-09-28T14:26:00Z")

</div>

> [@Mason](#):
>
> `AMD Ryzen`

Again, x86 architecture. I assume that some of you have Apple silicon…

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 28, 2023, 2:42pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/82 "2023-09-28T14:42:15Z")

</div>

Is your system apple silicon? That’d make me even less likely to suspect your performance puzzle is related to memory layout (though of course it’s far from ruled out!), since MacOS with apple silicon has a rather complicated and different scheduler than what most x86 chips use (though Intel’s more recent CPUs may have some similar characteristics due to them also having heterogeneous cores). Apple Silicon’s mobile lineage really shows not only in having big and little cores, but also spikey overclocking behaviour, and lots of things where the system is trying to reason about how many resources to give a certain process, and trying to turbocharge what it suspects are short-lived, latency sensitive processes while deprioritizing what it thinks might be more throughput based, long lived processes.

MacOS is also notorious for spinning up and down highly resource intensive background processes.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 28, 2023, 2:42pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/83 "2023-09-28T14:42:18Z")

</div>

> [@user664303](#):
>
> This is why I felt that an MWE was not useful. I’m also disappointed that having provided a MWE and indeed boxplots of the results, there is still scepticism.

On the contrary, the MWE was very enlightening. People were able to attempt it and verify the machine-dependence, and people could suggest changes. I especially appreciate the boxplot graph variant, it’s much clearer than a few `@benchmark` printouts. The skepticism is just part of fact-finding. Just because people have an open mind about alternative causes and confounding factors doesn’t mean they aren’t exploring the cause you want to address. In fact, all suggestions so far explore memory layout.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 28, 2023, 2:47pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/84 "2023-09-28T14:47:37Z")

</div>

> [@user664303](#):
>
> This is why I felt that an MWE was not useful. I’m also disappointed that having provided a MWE and indeed boxplots of the results, there is still scepticism.

As a moderator here, I’d like to try to de-escalate a bit. Please consider what want you’d want from this thread. IMV, the amount of engagement you’ve gotten here is _great_. Yes, there’s skepticism, but people are engaging. And that’s _far_ better than someone tacitly saying “ok, cool story” and moving on.

This is could — and should — be a fun “whodunnit” when approached with an open mind, curiosity, and respect for others. Let’s please put a pin in trying to police the commentary itself.

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [September 28, 2023, 2:51pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/85 "2023-09-28T14:51:53Z")

</div>

Yes, my system has an M1 Pro CPU.

> [@Mason](#):
>
> MacOS with apple silicon has a rather complicated and different scheduler than what most x86 chips use

This issue is not related to scheduling, as I understand, but related to cache line conflicts, branch prediction conflicts and such like.

> [@Mason](#):
>
> Apple Silicon’s mobile lineage really shows not only in having big and little cores, but also spikey overclocking behaviour, and lots of things where the system is trying to reason about how many resources to give a certain process. MacOS is also notorious for spinning up and down highly resource intensive background processes

I think you’re focusing on the prior, and not on the data. I have shown several experiments where there is no significant change in run time across multiple benchmarks. And I have shown several where there is. If these other factors were at play, as you suggest, then I would see this behaviour across every single benchmark I run - not just the ones where I claim there is an issue. I also return to another point I made before - how does your proposition make the change in run time happen precisely between runs of `@benchmark`, and not during?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 28, 2023, 2:52pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/86 "2023-09-28T14:52:06Z")

</div>

> [@mbauman](#):
>
> This is could — and should — be a fun “whodunnit” when approached with an open mind, curiosity, and respect for others. Let’s please put a pin in trying to police the commentary itself.

Well, I’d actually disagree a bit there. I mean, that’s what **most** people here want out of this thread, but it’s somewhat explicitly not what @user664303 wanted out of it, they mostly were posting about how they wanted a better way for BenchmarkTools.jl to account for memory alignment and make more comparable benchmarks.

Most people’s attitude seems to be “yeah that’d be nice but it’s hard enough to do that it’s not really actionable. But anyways, lets dig into what may or may not actually be happening here since there are good reasons you might have mistaken beliefs about the cause of this”

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [September 28, 2023, 2:54pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/87 "2023-09-28T14:54:11Z")

</div>

> [@Mason](#):
>
> that’s what **most** people here want out of this thread, but it’s somewhat explicitly not what @user664303 wanted out of it, they mostly were posting about how they wanted a better way for BenchmarkTools.jl to account for memory alignment and make more comparable benchmarks.

💯

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [September 28, 2023, 2:57pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/88 "2023-09-28T14:57:31Z")

</div>

> [@Mason](#):
>
> they mostly were posting about how they wanted a better way for BenchmarkTools.jl to account for memory alignment and make more comparable benchmarks.

And on that subject, I did find that someone is maintaining the Stabilizer tool, and posed [this question](https://github.com/Dead2/stabilizer/issues/32) to them.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 28, 2023, 3:00pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/89 "2023-09-28T15:00:42Z")

</div>

Why code reloading may cause the issue but regenerating the data doesn’t?

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [September 28, 2023, 3:02pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/90 "2023-09-28T15:02:16Z")

</div>

> [@user664303](#):
>
> I’d love to take another poll. Answers gratefully received. 🙏

Please do take the poll. The last one was good.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 28, 2023, 3:03pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/91 "2023-09-28T15:03:57Z")

</div>

> [@user664303](#):
>
> This issue is not related to scheduling, as I understand, but related to cache line conflicts, branch prediction conflicts and such like.

> [@user664303](#):
>
> I think you’re focusing on the prior, and not on the data. I have shown several experiments where there is no significant change in run time across multiple benchmarks. And I have shown several where there is. If these other factors were at play, as you suggest, then I would see this behaviour across every single benchmark I run - not just the ones where I claim there is an issue.

I guess I’d just say that as this thread progresses, I’m increasingly **less** convinced that alignment is the relevant variable here and more convinced that it’s the scheduler, and that’s not because of the prior but because of the data you and others have shown.

Maybe you could help me out by explaining what is making you think this can’t possibly be related to your computer’s rather different scheduler, heuristics, and your OS’s rather different approach to resource intensive background tasks?

> [@user664303](#):
>
> I also return to another point I made before - how does your proposition make the change in run time happen precisely between runs of `@benchmark`, and not during?

Because your CPU scheduler sees those `@benchmark` runs as separate work units. It can and does make guesses about what to do with an individual function and weighs that infromation against all sorts of opaque things like the current thermal conditions, what other programs are scheduled, whether or not Mercury is in retrograde, etc.

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [September 28, 2023, 3:04pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/92 "2023-09-28T15:04:26Z")

</div>

> [@lmiq](#):
>
> Why code reloading may cause the issue but regenerating the data doesn’t?

Good question. I’d be guessing… Perhaps branch prediction conflicts, perhaps changing the stack address slightly. Who knows.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 28, 2023, 3:06pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/93 "2023-09-28T15:06:15Z")

</div>

Though I should reiterate that I agree with your original premise regardless , because it’d be good if BenchmarkTools.jl were able to isolate things like alignment because it would make it much easier to know whether or not that was causing the problem!

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [September 28, 2023, 3:06pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/94 "2023-09-28T15:06:23Z")

</div>

> [@Mason](#):
>
> Because your CPU scheduler sees those `@benchmark` runs as separate work units. It can and does make guesses about what to do with an individual function and weighs that infromation against all sorts of opaque things like the current thermal conditions, what other programs are scheduled, whether or not Mercury is in retrograde, etc.

Now that were running 20 calls to @benchmark in a loop, how can the scheduler determine that they’re different runs?

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [September 28, 2023, 3:08pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/95 "2023-09-28T15:08:29Z")

</div>

> [@Mason](#):
>
> Maybe you could help me out by explaining what is making you think this can’t possibly be related to your computer’s rather different scheduler, heuristics, and your OS’s rather different approach to resource intensive background tasks?

One reason is that the timing always changes between code reloads. See the point I make above. Another is that my code is intensive and single threaded. There isn’t much else going on on my machine. It will just get thrown onto a high power core and run at full throttle.

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [September 28, 2023, 3:09pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/96 "2023-09-28T15:09:36Z")

</div>

> [@Mason](#):
>
> Though I should reiterate that I agree with your original premise regardless , because it’d be good if BenchmarkTools.jl were able to isolate things like alignment because it would make it much easier to know whether or not that was causing the problem!

It’s funny that we’re still talking about my code/results, though, isn’t it. 😆

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 28, 2023, 3:11pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/97 "2023-09-28T15:11:05Z")

</div>

> [@user664303](#):
>
> Now that were running 20 calls to @benchmark in a loop, how can the scheduler determine that they’re different runs?

Because every time there’s a non-inlined function call is seen as an opportunity for the scheduler to make new guesses about what to do.

Maybe it’d help if I clarified that I’m not talking about some high level task scheduler that just sees `julia` and a usage stat. There are very low level schedulers which are used for heterogeneous architectures like Apple Silicon that are making decisions based on incredibly granular information like what the bundle of instructions loaded into the cache are.

> [@user664303](#):
>
> There isn’t much else going on on my machine. It will just get thrown onto a high power core and run at full throttle.

That is very much not the case generically.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 28, 2023, 3:25pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/98 "2023-09-28T15:25:37Z")

</div>

I assume reloading the code requires the setup methods to be recompiled so you have a bunch of new addresses to cache. CPUs cache instructions and data separately. I assume this is why Stabilizer randomizes code, stack, and heap independently.

> [@Mason](#):
>
> more convinced that it’s the scheduler

this poked at my memory a bit, and the Stabilizer technical paper does mention another study where the environment affected job scheduling. Not sure if that’s related but after seeing a surname affect caching, I won’t be surprised if memory layout does somehow affect scheduling, maybe do jumping jacks

> [@user664303](#):
>
> Another is that my code is intensive and single threaded. There isn’t much else going on on my machine. It will just get thrown onto a high power core and run at full throttle.

That is not my understanding. Multitasking OSs have so many processes they will [interrupt a single-threaded program and restart it on a separate core.](https://superuser.com/questions/1680045/why-is-this-single-threaded-program-using-all-cpu-cores)

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [September 28, 2023, 5:34pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/99 "2023-09-28T17:34:13Z")

</div>

> [@Mason](#):
>
> Maybe it’d help if I clarified that I’m not talking about some high level task scheduler that just sees `julia` and a usage stat. There are very low level schedulers which are used for heterogeneous architectures like Apple Silicon that are making decisions based on incredibly granular information like what the bundle of instructions loaded into the cache are.

My machine has 9 cores. Here is the usage when I’m running the code:

 ![Screenshot 2023-09-28 at 17.19.59](https://global.discourse-cdn.com/julialang/original/3X/a/c/ac2aa23ece4e70123c976451a2c17d66a6e7f5ba.png)  
There is basically nothing else going on. Just for you, I’ve started Julia with:

```julia
sudo nice -n -20 julia

```

This sets the Julia process to the highest priority possible. Given these two things, the idea that something would interrupt the Julia process seems remote. But most of the things being suggested here seem remote to me.

Here’s the result of the code that I say is affected by memory layout:

 ![Screenshot 2023-09-28 at 18.22.53](https://global.discourse-cdn.com/julialang/original/3X/4/5/452055604cd7512005433cbd61feece230d4858d.png)

Now here’s the result of the code that just does a small matrix multiply a load of times:

 ![Screenshot 2023-09-28 at 18.32.27](https://global.discourse-cdn.com/julialang/original/3X/a/a/aa33d036f3700cf9e9c26948a075a7c6850fe5fc.png)

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 28, 2023, 5:45pm UTC](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256/100 "2023-09-28T17:45:03Z")

</div>

One more hypothesis: the MWE is using StaticArrays, which does a number of hairy things that are not 100% kosher. There’s a chance of some nasal demons flying around. I’ve seen some funny performance things with `@pure` in the past, for example.

[Previous page](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256.md?page=4)

[Next page](https://discourse.julialang.org/t/making-benchmark-outputs-statistically-meaningful-and-actionable/104256.md?page=6)
