lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2009/11/12 Matthew Wild <mwild1@gmail.com>:
> 2009/11/12 Mike Pall <mikelu-0911@mike.de>:
>> Matthew Wild wrote:
>>> I've come into a little debate recently. We're working on an API,
>>> which will be used to create *lots* of small objects. Performance is
>>> critical above everything else (don't shout at me for this :) ).
>>
>> Measure first, then optimize.
>>

Someone pointed me to this page on the wiki, which is interesting:
http://lua-users.org/wiki/ObjectBenchmarkTests

I added the following code:
----------------
do local data = 0;
        addbenchmark("Closure object (best case)", "ob('test')",
                function (method)
                        if method == "test" then
                                data = data + 1;
                        end
                end);
end

do local data = 0;
        addbenchmark("Closure object (worst case)", "ob('test')",
                function (method)
                        if method == "test9" then
                        elseif method == "test8" then
                        elseif method == "test7" then
                        elseif method == "test6" then
                        elseif method == "test5" then
                        elseif method == "test4" then
                        elseif method == "test3" then
                        elseif method == "test2" then
                        elseif method == "test" then
                                data = data + 1;
                        end
                end);
end
-------------------------

And since no thread here would be complete without benchmarks, here's
what I get (lower is better):

Plain Lua:
$ lua objtest.lua
26.34	Standard (solid)
29.2	Standard (metatable)
28.11	Object using closures (PiL 16.4)
16.51	Object using closures (noself)
12.29	Direct Access
2.45	Local Variable
16.09	Closure object (best case)
40.23	Closure object (worst case)

LuaJIT 1.x:
$ luajit -O3 objtest.lua
3.97	Standard (solid)
8.18	Standard (metatable)
4.86	Object using closures (PiL 16.4)
3.01	Object using closures (noself)
2.08	Direct Access
0.62	Local Variable
3.12	Closure object (best case)
4.28	Closure object (worst case)

LuaJIT 2.x *without* JIT:
$ luajit-2.0.0-beta1 -joff objtest.lua
6.55	Standard (solid)
12.64	Standard (metatable)
6.89	Object using closures (PiL 16.4)
5.87	Object using closures (noself)
3.16	Direct Access
0.91	Local Variable
4.35	Closure object (best case)
7.96	Closure object (worst case)

LuaJIT 2.x *with* JIT:
$ luajit-2.0.0-beta1 objtest.lua
0.19	Standard (solid)
0.19	Standard (metatable)
0.18	Object using closures (PiL 16.4)
0.19	Object using closures (noself)
0.19	Direct Access
0.19	Local Variable
0.18	Closure object (best case)
0.19	Closure object (worst case)

... which gives quite an interesting insight. And indeed, can't beat
Lua's hash lookups. Much :)

Matthew