# Performance comparison, Julia is a slower than Mojo, am I doing something wrong?

**URL:** <https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870>\
**Category:** Performance\
**Tags:** question, mojo\
**Created:** [September 15, 2023, 1:47am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870 "2023-09-15T01:47:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [September 15, 2023, 1:47am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/1 "2023-09-15T01:47:31Z")

</div>

I am using `fibonacci` function to the compare the performance between Julia and Mojo.  
First Julia code:

```julia
function fib(n::Int64)::Int64
    n <= 2 && return 1
    n == 3 && return 2
    fib(n - 1) + fib(n - 2)
end

@time fib(50);
 21.564018 seconds

```

Then Mojo code:

```julia
from time import now

fn fib(n:Int) -> Int:
    if n <= 2:
        return 1
    if n == 3:
        return 2
    return fib(n - 1) + fib(n - 2)

var eval_begin = now()
var res = fib(50)
var eval_end = now()
let execution_time_sequential = Float64((eval_end - eval_begin))
print("execution_time sequential in ms:")
print(execution_time_sequential / 1000000)
execution_time sequential in ms:
14253.018341999999

```

Julia 21s, Mojo 14s (Rust 16s), I can’t believe it, am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 15, 2023, 1:54am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/2 "2023-09-15T01:54:54Z")

</div>

This isn’t very surprising to me. Julia has a small amount of extra overhead on non-inlined function calls (for GC housekeeping). This tends to only matter for very small recursive functions since otherwise a function this small would have been inlined anyway. As such this benchmark doesn’t do a good job of measuring the performance of normal code.

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [September 15, 2023, 2:19am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/3 "2023-09-15T02:19:49Z")

</div>

If you aren’t using 1.10, I would test it because it reduces this overhead quite a bit.

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [September 15, 2023, 2:21am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/4 "2023-09-15T02:21:41Z")

</div>

The Julia version I’m using is 1.9.3.

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [September 15, 2023, 2:25am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/5 "2023-09-15T02:25:03Z")

</div>

I get about 15.5s on 1.10, which is pretty comparable.

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [September 15, 2023, 2:27am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/6 "2023-09-15T02:27:06Z")

</div>

But we have different machine, do your computer have mojo?

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [September 15, 2023, 3:00am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/7 "2023-09-15T03:00:22Z")

</div>

> I get about 15.5s on 1.10, which is pretty comparable.

How can you expect ordinary user to use Julia 1.10 when it is not out yet **AND** is not approved for production critical results.

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [September 15, 2023, 3:05am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/8 "2023-09-15T03:05:34Z")

</div>

> But we have different machine, do your computer have mojo?

Good point 🙂 No I can not get Mojo. Just wanted to verify the comment from @gbaraldi about this reducing the overhead (I also get about 21s on 1.9).

> How can you expect ordinary user to use Julia 1.10 when it is not out yet **AND** is not approved for production critical results.

I’m not expecting anything of anyone.

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [September 15, 2023, 3:16am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/9 "2023-09-15T03:16:12Z")

</div>

Hey @Brian1,

Not sure if it will change too much, but could you report back statistics with your fibonacci function using BenchmarkTools’s `@btime` macro? Like this?

```julia
using BenchmarkTools

@btime fib(50)

```

I agree with @Oscar_Smith , shouldn’t matter much but I am just curious.

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [September 15, 2023, 3:27am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/10 "2023-09-15T03:27:31Z")

</div>

It is the same as before.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [September 15, 2023, 3:39am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/11 "2023-09-15T03:39:13Z")

</div>

> [@StevenSiew](#):
>
> How can you expect ordinary user to use Julia 1.10 when it is not out yet **AND** is not approved for production critical results.

In case this was a sincere question, `juliaup` actually makes it quite straightforward to try out 1.10 🙂

In either case, I sincerely hope that neither of those `fib` implementations are used in production-critical contexts!

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [September 15, 2023, 8:18am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/12 "2023-09-15T08:18:18Z")

</div>

One of the things julia lacks is tail call optimization unlike (maybe) mojo, AFAIK.

---

<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 15, 2023, 8:45am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/13 "2023-09-15T08:45:26Z")

</div>

OP’s implementation [could not benefit from tail call optimization](https://steven-giesel.com/blogPost/ccdbefd9-2875-49e6-929c-c5081d5b4d27), there are 2 recursive calls followed by an addition prior to the return. If that link dies then here’s a Julia version of the implementation that does have a tail call:

```julia
"""
FibonacciTailRecursive(n::Int, previous::Int=0, current::Int=1) -> m+n-th number

previous is intended to be the m-th number, and current is intended to be the
m+1-th number. The 0th number is 0.
"""
function FibonacciTailRecursive(n::Int, previous::Int=0, current::Int=1)
    if n == 0 return previous end
    if n == 1 return current end
    FibonacciTailRecursive(n - 1, current, previous + current)
end

```

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [September 15, 2023, 9:16am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/14 "2023-09-15T09:16:21Z")

</div>

It is still tail call but not tail-recursive call in that definition.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 15, 2023, 10:40am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/15 "2023-09-15T10:40:28Z")

</div>

I get 29 seconds on 1.9.3, and 33 on 1.10.0-beta2, and pretty much the same as 1.10 on `master`.

But just to say how much useful is this benchmark, try

> [@Simple recursive Fibonaci example. How to make it faster?](https://discourse.julialang.org/t/simple-recursive-fibonaci-example-how-to-make-it-faster/32369/15):
>
> As noted above, it is probably TCO and/or unrolling. We can also do all kinds of tricks, eg function fibval(::Val{N}) where N if N ≤ 1 1 else fibval(Val{N - 1}()) + fibval(Val{N - 2}()) end end julia\> @time fibval(Val(46)) # first run, includes complilation time 0.057667 seconds (166.50 k allocations: 9.972 MiB, 22.78% gc time) julia\> @time fibval(Val(46)) # let's benchmark Base.show 0.000182 seconds (4 allocations: 160 bytes) These things are fun, but they t…

---

<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 15, 2023, 11:27am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/16 "2023-09-15T11:27:07Z")

</div>

To answer the question directly, no @Brian1, you are not doing anything incorrect. Run-time recursion in julia is not something that’s designed to be used in performance sensitive scenarios, and is instead optimized for different things.

It’s not particularly surprising that another language is faster than Julia at doing this. But this also isn’t a very big deal because any algorithm you want to write which makes use of recursion in this way can be rewritten in more performance friendly ways which we do expect Julia to perform well at.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [September 15, 2023, 11:47am UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/17 "2023-09-15T11:47:13Z")

</div>

Is this true?!!!

```julia
  | | |_| | | | (_| | | Version 1.10.0-beta2 (2023-08-17)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> function FibonacciTailRecursive(n::Int, previous::Int=0, current::Int=1)
           if n == 0 return previous end
           if n == 1 return current end
           FibonacciTailRecursive(n - 1, current, previous + current)
       end
FibonacciTailRecursive (generic function with 3 methods)

julia> @time FibonacciTailRecursive(50)
  0.000002 seconds
12586269025

```

---

<div class="post-metadata">

**Author:** ![gcalderone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gcalderone/32/1539_2.png) [@gcalderone](https://discourse.julialang.org/u/gcalderone)\
**Post date:** [September 15, 2023, 12:00pm UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/18 "2023-09-15T12:00:34Z")

</div>

> [@StevenSiew](#):
>
> How can you expect ordinary user to use Julia 1.10 when it is not out yet **AND** is not approved for production critical results.

Has MoJo v1 been released? Is it approved for production critical results?

Benchmarking is interesting and fun, and probably is the first reason why people get interested in Julia. But too often they probe the tiniest possible detail, providing limited clues on the overall experience.

Still, this is something interesting to re-evaluate when Julia version \>= 1.10 is released.

---

<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 15, 2023, 12:16pm UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/19 "2023-09-15T12:16:29Z")

</div>

> [@joa-quim](#):
>
> Is this true?!!!

Yes, but note that `FibonacciTailRecursive` is algorithmically different from the naïve `fib` implementation. It’s not a better implementation of the same algorithm, but instead a smarter algorithm, so it’s not a very enlightening comparison if you’re trying to compare languages.

`fib(50)` ends up recursively calling the `fib` function `12586269025` times, whereas `FibonacciTailRecursive` only does `50` recursive calls.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [September 15, 2023, 12:35pm UTC](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870/20 "2023-09-15T12:35:56Z")

</div>

Yes, I read a bit about the tail call optimizations before and didn’t want to imply that this is the number to be compared with the Mojo’s one.

[Next page](https://discourse.julialang.org/t/performance-comparison-julia-is-a-slower-than-mojo-am-i-doing-something-wrong/103870.md?page=2)
