# 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:** 20\
**Page:** 6

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [October 11, 2023, 7:30pm UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/101 "2023-10-11T19:30:27Z")

</div>

What makes this boilerplate is not how you write/space it, but the fact that you need to write it at all, i.e., in Scala you just define

```plaintext
class Petness(prive: String, val readme: String, var writeme: String)

```

making

- `prive` private, i.e., no getter and setter
- `readme` read only, i.e., getter, but no setter
- `writeme` read and writable, i.e., getter and setter

Only when you need non-standard behaviour, you have to spell out getters and setters explicitly.

---

<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 11, 2023, 7:38pm UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/102 "2023-10-11T19:38:20Z")

</div>

> [@bertschi](#):
>
> the fact that you need to write it at all

Exactly! and Scala’s version will not compress anywhere near as much as the Java version, which is basically reflecting the fact that Scala probably doesn’t have a lot of useless fluff that you have to write or read when working with it. So smaller compression ratios == more human friendly code.

Ideally we’d look at a 3D plot with three dimensionless axes:

1. Runtime / Minimum Runtime
2. Compressed Size / Minimum Compressed Size
3. Original Size / Compressed Size

(all of them arranged to be O(1) for good implementations, and smaller is better)

---

<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 11, 2023, 8:58pm UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/103 "2023-10-11T20:58:10Z")

</div>

> [@ericphanson](#):
>
> (Also the data compression limit is the Shannon entropy, so there’s a nice information theoretic flavor to compression-based metrics).

Totally tangential and my information theory is quite rusty, but I’d say the interesting limit is the Kolmogorov complexity rather than the Shannon entropy. (Which is equally information theoretic flavored.)

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [October 11, 2023, 11:28pm UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/104 "2023-10-11T23:28:45Z")

</div>

Related work

> **[Measuring source code conciseness across programming languages using compression](https://arxiv.org/abs/2111.09728)**
>
> It is well-known, and often a topic of heated debates, that programs in some programming languages are more concise than in others. This is a relevant factor when comparing or aggregating volume-impacted metrics on source code written in a...

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [October 12, 2023, 12:07am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/105 "2023-10-12T00:07:59Z")

</div>

What do you guys think about this?

> <https://github.com/jinyus/related_post_gen/issues/194>
>
> Hi,
> 
> it seems that Julia solution uses \`StaticArrays.jl\` package which uses un…safe code https://github.com/JuliaArrays/StaticArrays.jl/blob/d80455d2e1aba57a6cf65cc8d787dc6474ec3980/src/MArray.jl#L25
> 
> Honestly my understanding of the source of the package is rather poor
> so maybe the unsafe part is not called in the solution.
> 
> But if the unsafe part is called then it probably violates the rules.

I agree that it is essential to be fair in the context of the rules - but for somebody who does not understand Julia at a deep enough level, I think it is understandable why one would consider that _unsafe_ means _unsafe_ and that is the end of it.

Besides what @jling already presented, are there some good arguments?

Or should we agree that, indeed, we are breaking the rules by using the `StaticArrays` package?

---

<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 12, 2023, 4:28am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/106 "2023-10-12T04:28:23Z")

</div>

Noice. That article basically says the same thing I did, which is that compression ratio is a dimensionless measure of verbosity. Cobol compresses about 20:1 and R compresses about 4:1. I wonder how much Julia compresses… I’d guess maybe even less than R.

---

<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:** [October 12, 2023, 4:31am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/107 "2023-10-12T04:31:23Z")

</div>

Here’s my current replacement for StaticArrays.jl:

```julia
abstract type Vector5{T} <: AbstractVector{T} end
Base.size(::Vector5) = (5,)
Base.IndexStyle(::Type{<: Vector5}) = IndexLinear()
Base.getindex(A::Vector5, i::Int) = 
    i == 1 ? A.a :
    i == 2 ? A.b :
    i == 3 ? A.c :
    i == 4 ? A.d :
    i == 5 ? A.e :
    throw(BoundsError(A, i))

mutable struct MVector5{T} <: Vector5{T}
    a::T
    b::T
    c::T
    d::T
    e::T
end
MVector5{T}() where T = MVector5{T}(zero(T), zero(T), zero(T), zero(T), zero(T))
Base.setindex!(A::MVector5, v, i::Int) =
    i == 1 ? A.a = v :
    i == 2 ? A.b = v :
    i == 3 ? A.c = v :
    i == 4 ? A.d = v :
    i == 5 ? A.e = v :
    throw(BoundsError(A, i))

struct SVector5{T} <: Vector5{T}
    a::T
    b::T
    c::T
    d::T
    e::T
end
SVector5{T}(nt::NTuple{5,T}) where T =
    SVector5{T}(nt...)
SVector5(av::AbstractVector{T}) where T =
    SVector5{T}(av...)

```

**edit:**

Some thoughts for improvement:

1. Would it be better to just use `getfield` and `setfield!` to implement `getindex` and `setindex!`?

---

<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 12, 2023, 4:41am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/108 "2023-10-12T04:41:30Z")

</div>

> [@algunion](#):
>
> I agree that it is essential to be fair in the context of the rules - but for somebody who does not understand Julia at a deep enough level, I think it is understandable why one would consider that _unsafe_ means _unsafe_ and that is the end of it.

They’re free to choose their rules. It’s not clear to me if C would be allowed as a language, I guess since it doesn’t have any checks they aren’t “disabled”? Seems arbitrary to me.

I think I’d personally prefer a rule something like no use of unsafe in code written by the submitter. They’ve excluded “standard library” already, So there’s some arbitrary boundary set by whether the language maintainers want it in the “standard” library or in some “external package”. Julia chooses to push stuff into external packages for good reasons. But StaticVector is still a “standard package” and the code isn’t unsafe (ie. it does bounds check).

---

<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:** [October 12, 2023, 5:03am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/109 "2023-10-12T05:03:01Z")

</div>

New pull request submitted, removing StaticArrays.jl:

> <https://github.com/jinyus/related_post_gen/pull/200>
>
> This entirely removes StaticArrays.jl from the Julia code. Instead, we implement… new {M,S}Vector5 types specialized for mutable or immutable 5-element collections.
> 
> Also, this replaces manual package management with Project.toml files including the package UUIDs, adding specificity and additional security.
> 
> Fixes #194

---

<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:** [October 12, 2023, 7:29am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/110 "2023-10-12T07:29:27Z")

</div>

My simple static vector implementation may need more optimization, but my current computer is not quite up to the task.

I also created a version that uses a package so that the overall benchmarking time is reduced due to pkgimages:

> <https://github.com/mkitti/related_post_gen/pull/1>
>
> \`\`\`
> ~/src/related\_post\_gen$ ./run.sh julia
> Running Julia
> Julia:
> 
> Benchmark …1: julia --project related.jl
> Processing time (w/o IO): 48 milliseconds
> Processing time (w/o IO): 48 milliseconds
> Processing time (w/o IO): 48 milliseconds
> Processing time (w/o IO): 50 milliseconds
> Processing time (w/o IO): 48 milliseconds
> Processing time (w/o IO): 48 milliseconds
> Processing time (w/o IO): 49 milliseconds
> Processing time (w/o IO): 48 milliseconds
> Processing time (w/o IO): 49 milliseconds
> Processing time (w/o IO): 48 milliseconds
> Processing time (w/o IO): 48 milliseconds
> Processing time (w/o IO): 49 milliseconds
> Processing time (w/o IO): 48 milliseconds
> Time (mean ± σ): 651.2 ms ± 13.6 ms \[User: 547.7 ms, System: 288.9 ms\]
> Range (min … max): 634.2 ms … 669.5 ms 10 runs
>  
> Checking output
> Success: related\_posts\_julia.json is valid!
> 
> ~/src/related\_post\_gen$ ./run.sh julia\_con
> Running Julia Concurrent
> Julia Concurrent:
> 
> Benchmark 1: julia --project --threads auto related.jl
> Processing time (w/o IO): 22 milliseconds
> Processing time (w/o IO): 22 milliseconds
> Processing time (w/o IO): 23 milliseconds
> Processing time (w/o IO): 22 milliseconds
> Processing time (w/o IO): 23 milliseconds
> Processing time (w/o IO): 22 milliseconds
> Processing time (w/o IO): 22 milliseconds
> Processing time (w/o IO): 23 milliseconds
> Processing time (w/o IO): 22 milliseconds
> Processing time (w/o IO): 22 milliseconds
> Processing time (w/o IO): 15 milliseconds
> Processing time (w/o IO): 22 milliseconds
> Processing time (w/o IO): 22 milliseconds
> Time (mean ± σ): 631.5 ms ± 10.1 ms \[User: 617.6 ms, System: 292.4 ms\]
> Range (min … max): 618.6 ms … 645.7 ms 10 runs
> \`\`\`

---

<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 12, 2023, 9:03am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/111 "2023-10-12T09:03:32Z")

</div>

> [@mkitti](#):
>
> `mutable struct MVector5{T} <: Vector5{T}`

this is not going to be fast. The reason `MVector` was fast was due to its use of `NTuple`, and thus the need for `unsafe_*`.

What we need here is a built-in fixed-size array backed by stack allocation when it’s small

---

<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:** [October 12, 2023, 9:20am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/112 "2023-10-12T09:20:25Z")

</div>

> [@jling](#):
>
> this is not going to be fast. The reason `MVector` was fast was due to its use of `NTuple`, and thus the need for `unsafe_*`.

Not really, no. The reason `MVector` needed to do those pointer calls is that we don’t have a way of mutating tuples. i.e. it’s a difference between

```julia
mutable struct V1{N, T}
    v::NTuple{N, T}
end

```

and

```julia
mutable struct V2{N, T}
    v1::T
    v2::T
    v3::T
    ...
    vN::T
end

```

With `V1` we can only replace the entire `Tuple` at a time, not the individual elements of tuple without resorting to pointer tricks. We unfortunately can’t make the second one for arbitrary sizes, but if we could that’s how `MVector` would be implemented.

---

<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 12, 2023, 9:37am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/113 "2023-10-12T09:37:44Z")

</div>

Then why is it slow?

---

<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:** [October 12, 2023, 10:01am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/114 "2023-10-12T10:01:28Z")

</div>

Because there’s a ton of other infrastructure built around StaticArrays.jl, and for the most part Mark’s implementation is just hitting default defined methods that aren’t taking advantage of the fixed size. Also the `getindex` and `setindex!` methods were using `if-else` branching which is not ideal, and also wasn’t eligible remove the error branch.

---

<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:** [October 12, 2023, 10:47am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/115 "2023-10-12T10:47:06Z")

</div>

One thing that interests me here is that unlike StaticArrays.jl we do not have to solve the general problem. We know `n = 5`.

My current suspicion is that some of the important missing interface is the broadcasting.

> <https://github.com/mkitti/related_post_gen/blob/c1495369e79520dc0afd06decbef92c912009b7a/julia/related.jl#L39-L40>

You can see that StaticArrays.jl has a lot of machinery here:

> <https://github.com/JuliaArrays/StaticArrays.jl/blob/master/src/broadcast.jl>

---

<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 12, 2023, 10:49am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/116 "2023-10-12T10:49:06Z")

</div>

try manually override `fill!()` and use `fill!()`

```julia
function fill!(ary::MVector5, val)
     for i in eachindex(ary)
          ary[i] = val
     end
end

```

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [October 12, 2023, 11:15am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/117 "2023-10-12T11:15:57Z")

</div>

I don’t think the _unsafe_ related rules are sustainable anymore.

> <https://github.com/jinyus/related_post_gen/issues/203>
>
> After the discussion \[here \](https://github.com/jinyus/related\_post\_gen/issues/1…94), I think it starts to be clear that the \_unsafe\_ prohibition from any user library is going to be a difficult thing to uphold while also ensuring fairness.
> 
> I see a few challenges here:
> 
> \- The developer challenge: is everybody willing to provide an implementation here under the obligation to invest a tremendous amount of time and check every nook and cranny of the packages he uses so a case can be made that there is no \_usafe\_ code?
> \- The watch over the fence issue: I don't like that Rust is fast. I feel that I have reached the limits of my language, so let's take a peak then - Rust uses \`bumpalo\`, which contains unsafe code: owner, please ban \`bumpalo\`. To some extent, it seems that some developers are concerned with going over the source code of the packages used by the languages which happen to be at the top. So now we have the non-user of language X reading the code implemented in language X and opening an issue. But then some experienced users and even language developers of X intervene and present an expertise-backed opinion, and they can be easily dismissed (because they are biased and it is natural to take the side of their language).
> \- The owner's challenge: the owner of the repository will not be able to invoke expertise in all the used languages and ensure a fair judgment over all these issues.  
> 
> So where is this going? Are we starting a \`rust + bumpalo\` round now?  
> 
> \*\*Maybe an objective solution is to call this the battle of STDLIBS and prohibit any usage of any external package (while also prohibiting any \_unsafe\_ call from stdlib).\*\* 
> 
> But if we do the above, then this benchmark is no longer real-world (because who would avoid battle-tested packages and start implementing functionality from scratch)?
> 
> So how about an even better one: prohibit the developers from doing \_unsafe\_ tricks, and let's call it an \*\*ecosystem benchmark\*\*.

---

<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 12, 2023, 11:24am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/118 "2023-10-12T11:24:17Z")

</div>

tbh, Go is safe and fast somehow, I would be happy to learn what Golang is doing that’s so good

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [October 12, 2023, 11:43am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/119 "2023-10-12T11:43:14Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/7/0/707838222eb9cd2092eba6833c320de30a5417e7.png)

This is from this Go commit 🙂 OMG

[Some cleanup and a more generalized approach with Insertion sort by DrBlury · Pull Request #204 · jinyus/related\_post\_gen (github.com)](https://github.com/jinyus/related_post_gen/pull/204/files/93cc29cb8bf46aeac051b9862b3fdca87c47bfc8..49501632d5d772e9304ae12d17f80eb20675c132)

---

<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:** [October 12, 2023, 11:44am UTC](https://discourse.julialang.org/t/funny-benchmark-with-julia-no-longer-at-the-bottom/104611/120 "2023-10-12T11:44:07Z")

</div>

It’s their benchmark, their rules. If they don’t want to see the actual fastest code for the problem, it’s their lost.

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

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