fib.lua

来自「这个是脚本引擎lua的例程,这是一个嵌入式的袖珍引擎」· LUA 代码 · 共 41 行

LUA
41
字号
-- 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 + =
减小字号Ctrl + -
显示快捷键?