diff options
| author | Lua Team <team@lua.org> | 2003-04-11 12:00:00 +0000 |
|---|---|---|
| committer | repogen <> | 2003-04-11 12:00:00 +0000 |
| commit | f0e4e22f5c119865eb5a8d3844a40df2d5980b3b (patch) | |
| tree | c4df063a747e9c99f8aba1678588a030993780a9 /test/fib.lua | |
| parent | 1981b7c90eb09e956e969cda5c473be4560af573 (diff) | |
| download | lua-github-f0e4e22f5c119865eb5a8d3844a40df2d5980b3b.tar.gz | |
Lua 5.05.0
Diffstat (limited to 'test/fib.lua')
| -rw-r--r-- | test/fib.lua | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/test/fib.lua b/test/fib.lua index d946946a..97a921b1 100644 --- a/test/fib.lua +++ b/test/fib.lua @@ -1,5 +1,6 @@ --- very inefficient fibonacci function +-- fibonacci function with cache +-- very inefficient fibonacci function function fib(n) N=N+1 if n<2 then @@ -9,31 +10,31 @@ function fib(n) end end --- a much faster cached version - +-- a general-purpose value cache function cache(f) local c={} return function (x) - local y=%c[x] + local y=c[x] if not y then - y=%f(x) - %c[x]=y + y=f(x) + c[x]=y end return y end end -function test(s) +-- run and time it +function test(s,f) N=0 - local c=clock() - local v=fib(n) - local t=clock()-c + local c=os.clock() + local v=f(n) + local t=os.clock()-c print(s,n,v,t,N) end -n=n or 24 -- for other values, do lua -e n=XX fib.lua +n=arg[1] or 24 -- for other values, do lua fib.lua XX n=tonumber(n) print("","n","value","time","evals") -test("plain") +test("plain",fib) fib=cache(fib) -test("cached") +test("cached",fib) |
