-- fibonacci function with cache12-- very inefficient fibonacci function3function fib(n)4N=N+15if n<2 then6return n7else8return fib(n-1)+fib(n-2)9end10end1112-- a general-purpose value cache13function cache(f)14local c={}15return function (x)16local y=c[x]17if not y then18y=f(x)19c[x]=y20end21return y22end23end2425-- run and time it26function test(s,f)27N=028local c=os.clock()29local v=f(n)30local t=os.clock()-c31print(s,n,v,t,N)32end3334n=arg[1] or 24 -- for other values, do lua fib.lua XX35n=tonumber(n)36print("","n","value","time","evals")37test("plain",fib)38fib=cache(fib)39test("cached",fib)404142