lua-users home
lua-l archive

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


On Thu, 14 Aug 2014 03:23:57 +0200
Jan Behrens <jbe-lua-l@public-software-group.org> wrote:

> On Wed, 13 Aug 2014 19:02:25 -0300
> Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> 
> > > The behavior defined by the Lua 5.3.0 documentation, where the
> > > iteration runs from 1 to #t) looks acceptable to me, even though
> > > its implementation seems - by principle (as explained above) -
> > > inefficient to me.
> > 
> > Do you have any real data to back your worries?
> 
> It was an assumption until now. But I just made some benchmarks.
> (see below)
> 
> [...]
> 
> These are my test results:
> 
> ~/lua_ipairs_test/lua52 % time bin/lua ../test-array.lua
> 31.559u 0.007s 0:32.33 97.5%    233+172k 0+0io 0pf+0w
> ~/lua_ipairs_test/lua53 % time bin/lua ../test-array.lua
> 149.107u 0.007s 2:33.27 97.2%   259+172k 0+0io 6pf+0w
> ~/lua_ipairs_test/lua53compat % time bin/lua ../test-array.lua
> 30.539u 0.007s 0:31.17 97.9%    259+172k 0+0io 6pf+0w
> 
> ~/lua_ipairs_test/lua52 % time bin/lua ../test-table.lua
> 28.618u 0.000s 0:29.47 97.0%    233+172k 0+0io 3pf+0w
> ~/lua_ipairs_test/lua53 % time bin/lua ../test-table.lua
> 29.908u 0.000s 0:30.71 97.3%    258+172k 0+0io 0pf+0w
> ~/lua_ipairs_test/lua53compat % time bin/lua ../test-table.lua
> 29.900u 0.015s 0:30.54 97.9%    259+172k 0+0io 0pf+0w
> 
> 
> Thus, iteration without LUA_COMPAT_IPAIRS is almost 5 times slower when
> metamethods are used (149.107 seconds compared to 30.539 seconds).

For the sake of completeness, I should note that the speed difference
may be partly due to the fact that Lua 5.2's __ipairs mechanism allows
to directly use the shadow table as second value (state value) of the
iteration triplet. This speeds up things further, because the shadow
table that's attached to the proxy table doesn't need to be looked up
in every iteration step.

If I repeat the benchmark with a previous development version of the
JSON library where this trick was not applied yet and the iterator
function had to lookup the shadow table for every iteration step, then
I get the following results:

~/lua_ipairs_test/lua53 % time bin/lua ../test-array.lua
148.979u 0.015s 2:31.54 98.3%   259+172k 2+0io 8pf+0w
~/lua_ipairs_test/lua53compat % time bin/lua ../test-array.lua
46.142u 0.007s 0:46.91 98.3%    259+172k 0+0io 4pf+0w

http://www.public-software-group.org/mercurial/webmcp/file/9fd7e1bf9fe3/libraries/json/json.c

Here, we still have a speed difference of factor 3.


Regards
Jan Behrens