# Simple recursive Fibonaci example. How to make it faster?

**URL:** https://discourse.julialang.org/t/simple-recursive-fibonaci-example-how-to-make-it-faster/32369
**Category:** Performance
**Created:** [December 17, 2019, 2:59am UTC](https://discourse.julialang.org/t/simple-recursive-fibonaci-example-how-to-make-it-faster/32369 "2019-12-17T02:59:46Z")
**Posts on this page:** 1
**Showing post:** 16

<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: [December 17, 2019, 4:12pm UTC](https://discourse.julialang.org/t/simple-recursive-fibonaci-example-how-to-make-it-faster/32369/16 "2019-12-17T16:12:54Z")

</div>

Here’s how you can semi-manually get the tail call elimination.

```julia
julia> fib(n, a=0, b=1) = n > 0 ? fib(n-1, b, a+b) : a
fib (generic function with 3 methods)

julia> @btime fib($46)
  64.136 ns (0 allocations: 0 bytes)
1836311903

```

and the version compiled to an app:

```julia
[mason@mason-pc ~]$ cat Fib/src/Fib.jl
module Fib

fib(n, a=0, b=1) = n > 0 ? fib(n-1, b, a+b) : a

Base.@ccallable function julia_main()::Cint
    @show fib(46)
    return 0
end

end # module

[mason@mason-pc ~]$ julia -e 'using PackageCompilerX; create_app("Fib", "FibCompiled")'
  Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
 Resolving package versions...
  Updating `~/Fib/Project.toml`
 [no changes]
[ Info: PackageCompilerX: creating base system image (incremental=false)...
[ Info: PackageCompilerX: creating system image object file, this might take a while...
[ Info: PackageCompilerX: creating system image object file, this might take a while...

```

```julia
[mason@mason-pc ~]$ time FibCompiled/bin/Fib
fib(46) = 1836311903

real 0m0.330s
user 0m0.384s
sys 0m0.505s

```

but again, this is cheating since it’s not the algorithm asked for, even if other languages might have done something similar under the hood.

---

_[View the full topic](https://discourse.julialang.org/t/simple-recursive-fibonaci-example-how-to-make-it-faster/32369)._
