[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: parameter table
- From: Miles Bader <miles@...>
- Date: Sat, 19 Dec 2009 23:22:45 +0900
Incidentally, a simplistic benchmark:
$ time lua -e 'function f(a,b,c) return a+b+c end; sum = 0; for x = 1, 5000000 do sum = sum + f(x, x+1, x+2) end; print(sum)'
37500022500000
real 0m1.745s
user 0m1.352s
sys 0m0.000s
$ time lua -e 'function f(t) return t.a+t.b+t.c end; sum = 0; for x = 1, 5000000 do sum = sum + f{a=x, b=x+1, c=x+2} end; print(sum)'
37500022500000
real 0m3.844s
user 0m3.768s
sys 0m0.004s
The simple argument version is about 3 times faster (there would probably be less
somewhat advantage for more complex functions).
Here's the "optional table" version, which is sort of in between:
$ time lua -e 'function f(a,b,c) if type (a) == 'table' then b = a.b c = a.c a = a.a end return a+b+c end; sum = 0; for x = 1, 5000000 do sum = sum + f(x, x+1, x+2) end; print(sum)'
37500022500000
real 0m2.004s
user 0m2.000s
sys 0m0.004s
But with luajit, it's even more crazy (I guess since it's much easier to
optimize simple things like argument passing than table construction
etc...):
[note the loop count here is different than above]
$ time luajit-2.0.0-beta2 -e 'function f(a,b,c) return a+b+c end; sum = 0; for x = 1, 50000000 do sum = sum + f(x, x+1, x+2) end; print(sum)'
3.750000225e+15
real 0m0.244s
user 0m0.224s
sys 0m0.004s
$ time luajit-2.0.0-beta2 -e 'function f(t) return t.a+t.b+t.c end; sum = 0; for x = 1, 50000000 do sum = sum + f{a=x, b=x+1, c=x+2} end; print(sum)'3.750000225e+15
real 0m13.135s
user 0m12.989s
sys 0m0.000s
The simple argument version is almost 60 times faster!
With luajit, there's apparently little overhead from the "optional
table" code:
$ time luajit-2.0.0-beta2 -e 'function f(a,b,c) if type (a) == 'table' then b = a.b c = a.c a = a.a end return a+b+c end; sum = 0; for x = 1, 50000000 do sum = sum + f(x, x+1, x+2) end; print(sum)'
3.750000225e+15
real 0m0.238s
user 0m0.228s
sys 0m0.000s
-Miles
--
Innards, n. pl. The stomach, heart, soul, and other bowels.