# A problem about performance

**URL:** <https://discourse.julialang.org/t/a-problem-about-performance/88182>\
**Category:** Performance\
**Created:** [October 3, 2022, 7:20pm UTC](https://discourse.julialang.org/t/a-problem-about-performance/88182 "2022-10-03T19:20:23Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 3, 2022, 7:51pm UTC](https://discourse.julialang.org/t/a-problem-about-performance/88182/21 "2022-10-03T19:51:06Z")

</div>

> [@dlakelan](#):
>
> So you’re saying results don’t matter, only ideology matters.

what, when did I say that? stop making strawman argument

1. we know non-const global causes problem
2. some weird stuff going on in OP related to non-const global
3. solution: don’t use non-const global because it’s a bad idea in all cases anyway

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [October 3, 2022, 7:54pm UTC](https://discourse.julialang.org/t/a-problem-about-performance/88182/22 "2022-10-03T19:54:21Z")

</div>

Just pretend the first post says

“I wrote fun2 which is a good function, then I rewrite fun1 which passes the argument in a non const global variable… I thought it should be much slower but in fact it’s 3x faster. Why?”

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 3, 2022, 7:57pm UTC](https://discourse.julialang.org/t/a-problem-about-performance/88182/23 "2022-10-03T19:57:09Z")

</div>

Please tone it down here. No need to yell (type in all caps) or get short with anyone. Let’s focus on @dlakelan’s rephrased question here.

---

<div class="post-metadata">

**Author:** ![XuJingye2022](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xujingye2022/32/38972_2.png) [@XuJingye2022](https://discourse.julialang.org/u/XuJingye2022)\
**Post date:** [October 3, 2022, 8:06pm UTC](https://discourse.julialang.org/t/a-problem-about-performance/88182/24 "2022-10-03T20:06:03Z")

</div>

Love and peace 🫡

---

<div class="post-metadata">

**Author:** ![XuJingye2022](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xujingye2022/32/38972_2.png) [@XuJingye2022](https://discourse.julialang.org/u/XuJingye2022)\
**Post date:** [October 3, 2022, 8:07pm UTC](https://discourse.julialang.org/t/a-problem-about-performance/88182/25 "2022-10-03T20:07:38Z")

</div>

> [@dlakelan](#):
>
> I give up. The whole post is literally the OP asking why doing the wrong thing makes his code FASTER than doing the “right” thing.

It’s not “wrong”, just not the best.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [October 3, 2022, 8:07pm UTC](https://discourse.julialang.org/t/a-problem-about-performance/88182/26 "2022-10-03T20:07:49Z")

</div>

Thanks @StefanKarpinski and @jling I felt like simply this is a miscommunication. Hopefully the rephrased question helps.

Can anyone else confirm the results with the same Julia version as OP? AND OP what kind of computer are you running on? processor and OS etc?

Are you still able to consistently reproduce these timings?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [October 3, 2022, 8:19pm UTC](https://discourse.julialang.org/t/a-problem-about-performance/88182/27 "2022-10-03T20:19:41Z")

</div>

Easy heuristic: If a benchmark shows an elementary operation being way faster than 1 ns, you are probably not measuring anything meaningful.

Thought experiment: If your CPU can make 10000 additions in 1.8 ns with serial code, what kind of frequency does it need to run at? (Assume one operation per clock cycle unless you happen to know a better approximation.)

---

<div class="post-metadata">

**Author:** ![XuJingye2022](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xujingye2022/32/38972_2.png) [@XuJingye2022](https://discourse.julialang.org/u/XuJingye2022)\
**Post date:** [October 3, 2022, 8:22pm UTC](https://discourse.julialang.org/t/a-problem-about-performance/88182/28 "2022-10-03T20:22:29Z")

</div>

Because after I did what they said, `fun2($v)` or `const v = rand(10000)`, there was no difference between them, just like @lmiq 's. So it shouldn’t be a problem about version or platform.

I guess, `fun2` will do something extra compared with `fun1` when passing nonconstant `v` to them. I don’t know that, could someone explain that?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [October 3, 2022, 8:28pm UTC](https://discourse.julialang.org/t/a-problem-about-performance/88182/29 "2022-10-03T20:28:02Z")

</div>

If i make both functions return s at the end, then I get the same timing

```julia
@btime f1()
@btime f2($v)

```

Both take about 12us on my laptop.

So, I think the answer is probably that because the original functions return nothing the loop is optimized away, and the difference you saw was the difference in time it takes to call a function with a given single argument vs to call a zero argument function. That’s my guess.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [October 3, 2022, 8:39pm UTC](https://discourse.julialang.org/t/a-problem-about-performance/88182/30 "2022-10-03T20:39:00Z")

</div>

I think the difference is more a question of how effectively the two functions happen to be optimized down to doing nothing. You can see from the LLVM code in the original post that there are still remnants of doing something, which apparently takes a small amount of time, but that it clearly involves neither looping, nor summing anything.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [October 3, 2022, 8:40pm UTC](https://discourse.julialang.org/t/a-problem-about-performance/88182/31 "2022-10-03T20:40:15Z")

</div>

Yes, that seems to be it. As soon as you make them do useful work, by returning s at the end, they both do real stuff and take about the same time.

[Previous page](https://discourse.julialang.org/t/a-problem-about-performance/88182.md?page=1)
