📄 fib.lua
字号:
-- fibonacci function with cache-- very inefficient fibonacci functionfunction fib(n) N=N+1 if n<2 then return n else return fib(n-1)+fib(n-2) endend-- a general-purpose value cachefunction cache(f) local c={} return function (x) local y=c[x] if not y then y=f(x) c[x]=y end return y endend-- run and time itfunction test(s,f) N=0 local c=os.clock() local v=f(n) local t=os.clock()-c print(s,n,v,t,N)endn=arg[1] or 24 -- for other values, do lua fib.lua XXn=tonumber(n)print("","n","value","time","evals")test("plain",fib)fib=cache(fib)test("cached",fib)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -