aboutsummaryrefslogtreecommitdiff
path: root/libs/lua/test/fib.lua
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-05-16 10:58:21 (GMT)
committerPavel Labath <pavelo@centrum.sk>2011-06-15 15:33:48 (GMT)
commitd4c5d99bcbf614e31e4f724319437e82d7f2a208 (patch)
tree84028219313ee62cc4bcccec2e3e2604e56c9dac /libs/lua/test/fib.lua
parentdb243c1d343781a1466e1b863e174427cd37e37f (diff)
downloadfluxbox_pavel-d4c5d99bcbf614e31e4f724319437e82d7f2a208.zip
fluxbox_pavel-d4c5d99bcbf614e31e4f724319437e82d7f2a208.tar.bz2
Add lua as an internal library in libs/lua
Diffstat (limited to 'libs/lua/test/fib.lua')
-rw-r--r--libs/lua/test/fib.lua40
1 files changed, 40 insertions, 0 deletions
diff --git a/libs/lua/test/fib.lua b/libs/lua/test/fib.lua
new file mode 100644
index 0000000..97a921b
--- /dev/null
+++ b/libs/lua/test/fib.lua
@@ -0,0 +1,40 @@
1-- fibonacci function with cache
2
3-- very inefficient fibonacci function
4function fib(n)
5 N=N+1
6 if n<2 then
7 return n
8 else
9 return fib(n-1)+fib(n-2)
10 end
11end
12
13-- a general-purpose value cache
14function cache(f)
15 local c={}
16 return function (x)
17 local y=c[x]
18 if not y then
19 y=f(x)
20 c[x]=y
21 end
22 return y
23 end
24end
25
26-- run and time it
27function test(s,f)
28 N=0
29 local c=os.clock()
30 local v=f(n)
31 local t=os.clock()-c
32 print(s,n,v,t,N)
33end
34
35n=arg[1] or 24 -- for other values, do lua fib.lua XX
36n=tonumber(n)
37print("","n","value","time","evals")
38test("plain",fib)
39fib=cache(fib)
40test("cached",fib)