Simple recursive Fibonaci example. How to make it faster?

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 tell you little about real-life performance of 10K LOC applications, which is what matters in practice — and of course, developer time.

8 Likes