Defining Pet Features and Essentials

Geoff Canyon gcanyon at gmail.com
Wed Feb 12 22:48:05 EST 2014


On Tue, Feb 11, 2014 at 6:59 AM, Ender Nafi Elekcioglu <
endernafi at keehuna.com> wrote:

> Try to calculate a recursive fib(33) under 0.5 second
> {it's 14 seconds on a brand new Macbook Pro Retina, where it's ~100ms. for
> Javascript.}
>

I'll take that bet:

local fibArray

function fib N
   if N = 0 or N = 1 then return N
   if fibArray[N] is empty then put fib(N-2) + fib(N-1) into fibArray[N]
   return fibArray[N]
end fib

on mouseUp
   put the long seconds into T
   get fib(33)
   put it && the long seconds - T
end mouseUp

Puts:

3524578 0.000022

It would be *really* good if LC supported co-routines, tail-recursion
optimization, and native memo-ization of functions, but it's often not hard
to memo-ize manually. Of course in this case recursion is unhelpful to
begin with, but I'm pretending that it's unavoidable since it's the reason
for the example.



More information about the use-livecode mailing list