lua-users home
lua-l archive

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


Wim Couwenberg wrote:
Also, it seems like it would be more efficient to use a table directly for a couple of reasons. First, the special code that must have been in place to handle a table directly would be faster than having to make a function call every time.


I did a simple test. I timed the following scripts in 4.0.1, 5.0.2 and 5.1-alpha:

Lua 4.0.1 version:

    local t = {}
    for i = 1, 1e4 do t[i] = i*i end

    for i = 1, 1e4 do
      for k, v in t do end
    end

Lua 5.0.2 and 5.1-alpha version:

    local t = {}
    for i = 1, 1e4 do t[i] = i*i end

    for i = 1, 1e4 do
      for k, v in pairs(t) do end
    end

results:

Lua 4.0.1: ~7.3 seconds
Lua 5.0.2: ~15.2 seconds
Lua 5.1-alpha: ~16.1 seconds

That makes sense. In the first version, some implicit functions are being executed, which are faster than explicitly calling pairs(). Seems like a 2x improvement in this sort of thing is A Good Thing (tm).


--
chris marrin                ,""$,
chris@marrin.com          b`    $                             ,,.
                        mP     b'                            , 1$'
        ,.`           ,b`    ,`                              :$$'
     ,|`             mP    ,`                                       ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`
"As a general rule,don't solve puzzles that open portals to Hell"'