# Funny Benchmark with Julia (no longer) at the bottom

**URL:** <https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611>\
**Category:** Performance\
**Tags:** benchmark\
**Created:** [October 5, 2023, 1:12pm UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611 "2023-10-05T13:12:30Z")\
**Posts on this page:** 10\
**Page:** 8

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [November 1, 2023, 5:21pm UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/141 "2023-11-01T17:21:55Z")

</div>

I monitored the results of the last runs of the benchmark on the virtual machine, see e.g. [https://github.com/jinyus/related\_post\_gen/blob/main/raw\_results.md](https://github.com/jinyus/related_post_gen/blob/main/raw_results.md) or [https://github.com/jinyus/related\_post\_gen/blob/697198a2bab8c9b9e2f90fe6569cc6afbc80f772/raw\_results.md](https://github.com/jinyus/related_post_gen/blob/697198a2bab8c9b9e2f90fe6569cc6afbc80f772/raw_results.md), what I see is strange to me. Julia is the more performance-inconsistent between runs compared with all the other languages e.g. compare the results of Rust with the ones of Julia for the 5k posts run:

```julia
Rust

Benchmark 1: ./target/release/rust
Processing time (w/o IO): 23.541084ms
Processing time (w/o IO): 23.466582ms
Processing time (w/o IO): 23.441882ms
Processing time (w/o IO): 23.468383ms
Processing time (w/o IO): 23.629785ms
Processing time (w/o IO): 23.484583ms
Processing time (w/o IO): 23.434282ms
Processing time (w/o IO): 23.425681ms
Processing time (w/o IO): 23.420081ms
Processing time (w/o IO): 23.649885ms
Processing time (w/o IO): 23.493782ms
Processing time (w/o IO): 23.469783ms
Processing time (w/o IO): 23.456882ms
  Time (mean ± σ): 39.6 ms ± 0.8 ms [User: 32.2 ms, System: 7.4 ms]
  Range (min … max): 39.1 ms … 41.7 ms 10 runs

Julia

Benchmark 1: julia -O3 --project=Related -e "using Related; main()"
Processing time (w/o IO): 31 milliseconds
Processing time (w/o IO): 24 milliseconds
Processing time (w/o IO): 31 milliseconds
Processing time (w/o IO): 31 milliseconds
Processing time (w/o IO): 31 milliseconds
Processing time (w/o IO): 24 milliseconds
Processing time (w/o IO): 31 milliseconds
Processing time (w/o IO): 31 milliseconds
Processing time (w/o IO): 25 milliseconds
Processing time (w/o IO): 24 milliseconds
Processing time (w/o IO): 31 milliseconds
Processing time (w/o IO): 24 milliseconds
Processing time (w/o IO): 24 milliseconds
  Time (mean ± σ): 3.351 s ± 0.047 s [User: 3.200 s, System: 0.254 s]
  Range (min … max): 3.280 s … 3.447 s 10 runs

```

Interestingly **this happens also on my machine** by running in the REPL `using Related; main()`, restarting Julia can downgrade performance forever for that REPL session. And restarting again, can make the performance again better.

All other languages except for NodeJS (which has some less large oscillations) don’t exhibit these variations, so I’m asking myself what is special about Julia, the code run on the VM seems to be on this file [https://github.com/jinyus/related\_post\_gen/blob/main/run.sh:](https://github.com/jinyus/related_post_gen/blob/main/run.sh:)

```bash
run_julia() {
    echo "Running Julia" &&
        cd ./julia &&
        julia -e 'using Pkg; Pkg.activate("Related"); Pkg.instantiate()' &&
        if [$HYPER == 1]; then

            capture "Julia" hyperfine -r $runs -w $warmup --show-output "julia --project=Related -e \"using Related; main()\""
        else
            command ${time} -f '%es %Mk' julia --project=Related -e "using Related; main()"

        fi

    check_output "related_posts_julia.json"
}

```

I’m not sure what can cause this difference, do someone have an explaination for this behaviour?

---

<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:** [November 1, 2023, 6:53pm UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/142 "2023-11-01T18:53:52Z")

</div>

here’s something even more funny  
if you add

```julia
@compile_workload begin
    main()
end

```

you get

```julia
Benchmark 1: julia --startup-file=no --project=Related -e "using Related; main()"
Processing time (w/o IO): 222 milliseconds
Processing time (w/o IO): 21 milliseconds
Processing time (w/o IO): 22 milliseconds
Processing time (w/o IO): 22 milliseconds
Processing time (w/o IO): 22 milliseconds

```

but if you add (without `[1:2]`):

```julia
@setup_workload begin
    json_string = read(@ __DIR__ ()*"/../../../posts.json", String)
    posts = JSON3.read(json_string, Vector{PostData})

    @compile_workload begin
        related(posts)
    end
end

```

you get

```julia
Benchmark 1: julia --project=Related -e "using Related; main()"
Processing time (w/o IO): 21 milliseconds
Processing time (w/o IO): 22 milliseconds
Processing time (w/o IO): 23 milliseconds
Processing time (w/o IO): 24 milliseconds
Processing time (w/o IO): 23 milliseconds
Processing time (w/o IO): 21 milliseconds

```

---

<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:** [November 1, 2023, 7:24pm UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/143 "2023-11-01T19:24:36Z")

</div>

> <https://github.com/jinyus/related_post_gen/pull/400#issue-1972961020>
>
> tested on the same VM. I now suspect the vGPU has some policy if it sees you idl…e it will start shifting resource away from your VM or something
> 
> before:
> \`\`\`
> Benchmark 1: julia --project=Related -e "using Related; main()"
> Processing time (w/o IO): 31 milliseconds
> Processing time (w/o IO): 23 milliseconds
> Processing time (w/o IO): 24 milliseconds
> Processing time (w/o IO): 25 milliseconds
> Processing time (w/o IO): 30 milliseconds
> Processing time (w/o IO): 31 milliseconds
> Processing time (w/o IO): 25 milliseconds
> \`\`\`
> 
> after:
> \`\`\`
> Benchmark 1: julia --project=Related -e "using Related; main()"
> Processing time (w/o IO): 24 milliseconds
> Processing time (w/o IO): 25 milliseconds
> Processing time (w/o IO): 25 milliseconds
> Processing time (w/o IO): 24 milliseconds
> Processing time (w/o IO): 24 milliseconds
> Processing time (w/o IO): 24 milliseconds
> Processing time (w/o IO): 25 milliseconds
> Processing time (w/o IO): 24 milliseconds
> \`\`\`

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [November 1, 2023, 8:32pm UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/144 "2023-11-01T20:32:22Z")

</div>

Still I don’t see deterministic results on my machine, can [Precompilation does not produce a deterministic result · Issue #25900 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/25900) be the cause?

This is what I see restarting the REPL, retriggering precompilation (by modifying the source code without real changes, e.g. by adding a space) and running `main()`:

```julia
bob@bob-Victus-by-HP-Gaming-Laptop-15-fb0xxx:~$ julia # still needs precompilation

julia> using Related
[Info: Precompiling Related [892d0954-52c7-4a59-ae8f-9a164aba3227]
Processing time (w/o IO): 4586 milliseconds
julia> main()
Processing time (w/o IO): 267 milliseconds # it went good, consistently these results
13262912
julia> main()
Processing time (w/o IO): 272 milliseconds
13262912
julia> main()
Processing time (w/o IO): 265 milliseconds
13262912
bob@bob-Victus-by-HP-Gaming-Laptop-15-fb0xxx:~$ julia # restart

julia> using Related
julia> main()
Processing time (w/o IO): 268 milliseconds
13262912
julia> main()
Processing time (w/o IO): 269 milliseconds
13262912
julia> main()
Processing time (w/o IO): 269 milliseconds
13262912

bob@bob-Victus-by-HP-Gaming-Laptop-15-fb0xxx:~$ julia # just added a space in the related.jl file to trigger precompilation

julia> using Related
[Info: Precompiling Related [892d0954-52c7-4a59-ae8f-9a164aba3227]
Processing time (w/o IO): 4669 milliseconds
julia> main()
Processing time (w/o IO): 361 milliseconds # it went bad
13262912
julia> main()
Processing time (w/o IO): 364 milliseconds
13262912
julia> main()
Processing time (w/o IO): 360 milliseconds
13262912
julia> main()
Processing time (w/o IO): 360 milliseconds
13262912

bob@bob-Victus-by-HP-Gaming-Laptop-15-fb0xxx:~$ julia

julia> using Related
julia> main()
Processing time (w/o IO): 360 milliseconds # forever bad
13262912
julia> main()
Processing time (w/o IO): 361 milliseconds
13262912
julia> main()
Processing time (w/o IO): 358 milliseconds
13262912

bob@bob-Victus-by-HP-Gaming-Laptop-15-fb0xxx:~$ julia

julia> using Related
julia> main()
Processing time (w/o IO): 361 milliseconds
13262912
julia> main()
Processing time (w/o IO): 367 milliseconds
13262912

```

which is intriguing

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [November 1, 2023, 8:49pm UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/145 "2023-11-01T20:49:08Z")

</div>

I think it would be really interesting if someone else can reproduce this kind of behaviour with the branch of @jling just posted since to me this seems like a bug in precompilation

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [November 1, 2023, 11:37pm UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/146 "2023-11-01T23:37:51Z")

</div>

If Julia is precompiling everytime, then it is detecting heterogeneous CPU architectures, invalidating the prior CPU cache, and trying to target the current machine.

What kind of processor are you using? Could you show us the output of `versioninfo()` in the REPL?

You also mentioned a virtual machine? What kind of virtual machine?

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [November 2, 2023, 12:03am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/147 "2023-11-02T00:03:56Z")

</div>

The VM is a Azure F4s v2 - 4vCPU-8GB-Ubuntu 22.04.

My computer info are

```julia
julia> versioninfo()
Julia Version 1.9.3
Commit bed2cd540a1 (2023-08-24 14:43 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 12 × AMD Ryzen 5 5600H with Radeon Graphics
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, znver3)
  Threads: 1 on 12 virtual cores

```

Actually, the precompilation retriggering is done by me on purpose to show this strange behaviour, I just add a space somewhere so that the module recompiles. After that I can randomly get around 270 ms all the time or 360 ms all the time. And this is I think what happens also on that VM.

If this phenomenon was language agnostic I wouldn’t have thought about some problem with Julia itself, but on that VM the performance of all other languages are much less variant. While Julia has 30% variation, Rust has 1%. Actually, apart from the comparison between languages, having oscillations that big after retriggering precompilation seems already strange.

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [November 2, 2023, 1:19am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/148 "2023-11-02T01:19:47Z")

</div>

I reproduced on another computer, so I opened an issue for this [Oscillations in performance after different precompilation runs · Issue #51988 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/51988)

---

<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:** [November 3, 2023, 12:28pm UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/149 "2023-11-03T12:28:26Z")

</div>

You’re doing the lord’s work here, thanks for going to the bottom of things. I think this might be a counterargument to the “don’t participate in micro benchmarks” – sometimes we find very deep “bugs” only be focusing on small tasks you can afford to compare assembly.

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [November 4, 2023, 1:32pm UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/150 "2023-11-04T13:32:17Z")

</div>

Even though I tried many methods to debug this performance issue, I’m running a bit out of ideas, so I would be really grateful If someone else more expert on this sort of analysis could share some more insights. If someone is interested, I prepared two perf profiler runs, one slow and one fast, available at [https://github.com/Tortar/related\_post\_gen/blob/main/perf-jit-results.tar.xz](https://github.com/Tortar/related_post_gen/blob/main/perf-jit-results.tar.xz)

P.S: thanks @jling for the kind words 🙂

[Previous page](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611.md?page=7)
