# Programming Language Benchmark 2

**URL:** <https://discourse.julialang.org/t/programming-language-benchmark-2/108429>\
**Category:** Performance\
**Created:** [January 6, 2024, 10:01am UTC](https://discourse.julialang.org/t/programming-language-benchmark-2/108429 "2024-01-06T10:01:40Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [January 11, 2024, 9:24am UTC](https://discourse.julialang.org/t/programming-language-benchmark-2/108429/21 "2024-01-11T09:24:11Z")

</div>

At least _some_ of my understanding could yet survive the morning 😀.

Thanks!

---

<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:** [January 11, 2024, 9:31am UTC](https://discourse.julialang.org/t/programming-language-benchmark-2/108429/22 "2024-01-11T09:31:36Z")

</div>

> [@nsajko](#):
>
> ```julia-auto
> m = clamp(n, one(T)::T, T(20)::T)
> v = Vector{Float64}(undef, m)
> 
> ```

I suppose the intent here was to suggest to the compiler that `T(1) <= m <= T(20)` so the `Vector`’s normally unfixed size (unliked `MVector`) has a maximum that can be on the stack? I’m not sure if the compiler can ever tell that from `clamp` because it could be defined to do something different.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 11, 2024, 10:03am UTC](https://discourse.julialang.org/t/programming-language-benchmark-2/108429/23 "2024-01-11T10:03:46Z")

</div>

So why not using StaticArrays? There is not only `MVector`, but also `SizedVector`…

---

<div class="post-metadata">

**Author:** ![codehive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/codehive/32/206129_2.png) [@codehive](https://discourse.julialang.org/u/codehive)\
**Post date:** [January 11, 2024, 11:23am UTC](https://discourse.julialang.org/t/programming-language-benchmark-2/108429/24 "2024-01-11T11:23:17Z")

</div>

(OT): What I like about this repository is that the code samples do resemble examples of realistic uses of each respective languages.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [April 5, 2024, 12:43am UTC](https://discourse.julialang.org/t/programming-language-benchmark-2/108429/25 "2024-04-05T00:43:42Z")

</div>

I find different relative performance, just looking at two languages and two benchmarks

I modified the implementations to run the examples in a loop, like 5 or 10 repetitions. This helps Julia a bit relative to the static languages. But it makes it easier to benchmark. And my benchmark is just as meaningful as the original. (It does not change the relative results much.) I find

- nqueens. ratio of time Julia / Rust == 1
- mat\_mul ratio of time Julia/Rust == 0.77 (Julia is faster)

I don’t know what the relevant machine characteristics are. But:  
Intel(R) Core™ i9-9880H CPU @ 2.30GHz

rustc 1.75.0 (82e1608df 2023-12-21)  
julia 1.10.2

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [April 8, 2024, 5:30pm UTC](https://discourse.julialang.org/t/programming-language-benchmark-2/108429/26 "2024-04-08T17:30:11Z")

</div>

> [@jakobnissen](#):
>
> ```julia
> if y < 0
> x >> abs(y)
> 
> ```

This (unneeded branch) can be easily avoided by using an Unsigned type for the bitshift amount (getting us half way there to optimal, 1, not 2, branches and they cost; still by number of instructions, a less important metric not half way there, nor at optimal one instruction), but ironically this has two bugs:

> [@jakobnissen](#):
>
> ```julia
> elseif y > 8 * sizeof(x)
> zero(x)
> 
> ```

`sizeof` gives size in bytes (_usually_), and arguably would have been better in bits to not need `8*` because, that works for Int8 up to Int128 (but not BigInt, which is special-cased, since sizeof(BigInt) == 16, though not really… it’s going to stay that way for huge numbers, since this is really a pointer, and more, but not all the data).

I was thinking maybe we need new types in Base: `UInt6` for bitshift-count for Int64 (and Int on 64-bit), and `UInt5` for Int32 ( **and Int on 32-bit…** ), because the `% 63` trick I saw in a different thread (_for Int_) is problematic.

It works on your 64-bit platform, but it’s so easy to forget sometimes Julia code is run on a 32-bit CPU/platform, i.e. on such a platform you shouldn’t hard-code `63` in (your code didn’t, takes that into account and only has the BigInt bug; and potential bug for such new types). You guard away with `8 * sizeof(x)` (i.e. working for all built-in types, and this was only pseudo-code after all); and then in alternative fast code with the extra the `- 1`.

But if we had `UInt6` what is `sizeof(UInt6)`? Is it 1 (stored in one byte), since it must? Or arguably should be 0.75. But we can’t return a float, since we decided on an integer returned (we do not want type-unstable), and your code requires a fraction of 1 to be fully general.

Maybe we need `sizeof_in_bits` in Base, to think ahead until we get such smaller types, also useful for e.g. nucleotides, and 4-bit (and lower) quantization in neural networks.

> <https://github.com/BioJulia/BioSymbols.jl/blob/a35961c42bd91d784ae39f6630c3ede64f1910aa/src/nucleicacid.jl#L12-L19>

I would have thought 2-bit used actually, but we have 4-bit for on-hot encoded… Either way there’s one size _in number of bits_, and a different `sizeof`, not consistent, since most likely stored still in 1 byte (in memory, unless you ask for not to), or when in registers (then 8- or likely actually stored in 64-bit one).

```julia
julia> bitstring(typemax(Int64) << (63 & (8*sizeof(Int64) - 1)))
"1000000000000000000000000000000000000000000000000000000000000000"

```

Just note that the bitshift by (0 up to) 63 works there, until it doesn’t from 64 (also for all negative numbers), then the “trick” fails, but you are getting the semantics of the assembly instruction, so i.e. you are promising never to be out of 0…63 bounds. This is why I oppose an operator to make this easy, as in C, to make a mistake, ok with functional-form `unsafe_shift` (and/or a `UInt6` and `UInt5` type, then it’s up to you to use the type and correctly).

[Previous page](https://discourse.julialang.org/t/programming-language-benchmark-2/108429.md?page=1)
