lua-users home
lua-l archive

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


Am 18.12.2012 11:08, schrieb steve donovan:
On Mon, Dec 17, 2012 at 3:02 PM, steve donovan
<steve.j.donovan@gmail.com> wrote:
But we ought to measure these things first before speculating - I'll
check with an actual computer tomorrow.

In fact, I should check with a computer before even replying, because
the results were interesting.

[...]


I've also done some micro benchmarking:

Lua 5.2.1  Copyright (C) 1994-2012 Lua.org, PUC-Rio
 Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
5 runs for extend1 (5000000 iterations)
avg: 7.377 s
5 runs for extend2 (5000000 iterations)
avg: 8.062 s
5 runs for extend3 (5000000 iterations)
avg: 8.898 s

First lesson: Don't call it a million times per second :-p


steve d.



Philipp


Attachment: benchmark.sh
Description: application/shellscript

#!/usr/bin/lua


local assert = assert
local type = assert( type )
local select = assert( select )
local pairs = assert( pairs )
local tonumber = assert( tonumber )


local function extend1( t, a )
  if type( a ) == "table" then
    for k,v in pairs( a ) do
      t[ k ] = v
    end
  end
  return t
end


local function extend2( t, ... )
  for i = 1, select( '#', ... ) do
    local a = select( i, ... )
    if type( a ) == "table" then
      for k,v in pairs( a ) do
        t[ k ] = v
      end
    end
  end
  return t
end


local function extend3( t, ... )
  local arg = { ... }
  for i = 1, select( '#', ... ) do
    local a = arg[ i ]
    if type( a ) == "table" then
      for k,v in pairs( a ) do
        t[ k ] = v
      end
    end
  end
  return t
end


local extends = { extend1, extend2, extend3 }
local f = assert( extends[ arg[ 1 ] and tonumber( arg[ 1 ] ) or 1 ] )
local n = assert( arg[ 2 ] and tonumber( arg[ 2 ] ) )
local v = {
  a = 1,
  b = 2,
  c = 3,
  d = 4,
  e = 5,
  f = 6
}

for i = 1, n do
  f( {}, v )
end