lua-users home
lua-l archive

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


Mike Pall wrote:
Hi,

Mark Hamburg wrote:

Mike is correct that introducing the iterator function -- even as useful as
it is -- brings in memory allocation costs and hence is a lose for small
numbers of arguments.


So, let's benchmark it. Here are the two ways to iterate over
arguments (without doing anything with them):

local function iterate_with_select(...)
  local select = select
  for j=1,1e6 do

    for i=1,select('#', ...) do
      local a = select(i, ...)
    end

  end
end

Doesn't this have to call select('#',...) on every iteration? And since select is a function call, wouldn't:

    local n = select('#', ...)
    for i=1,n do
      local a = select(i, ...)
    end

give you a noticeable improvement?

--
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"'