Guest User

benchmark.lua

a guest
Jan 24th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.50 KB | None | 0 0
  1. local sum = 0   -- just for benchmark
  2.  
  3. -- insert elements into some object
  4. local function insert(obj, elem1, elem2, ...)
  5.    -- this function may have 1..inf actual arguments
  6.    if not elem2 and elem1 then
  7.       -- user invoked this function to insert single element
  8.       -- this is the most frequent usage of this function
  9.       -- for the sake of performance we avoid to create a table
  10.       ----------------------------------------------------------------
  11.       -- optimized code for inserting single element (elem1)
  12.       ----------------------------------------------------------------
  13.       sum = sum + elem1  -- this line is just for contrived benchmark
  14.    else  -- "else" branch is not being benchmarked
  15.       -- user invoked this function to insert multiple elements
  16.       -- we are creating a table containing named arguments and varargs
  17.       local elems = {elem1, elem2, ...}
  18.       for j = 1, #elems % 8 do
  19.          insert(obj, elems[j]) -- recursive call to insert one element
  20.       end
  21.       for j = #elems % 8 + 1, #elems, 8 do
  22.          local e1, e2, e3, e4, e5, e6, e7, e8 = unpack(elems, j, j + 7)
  23.          ----------------------------------------------------------------
  24.          -- inserting full block of eight elements
  25.          -- (it is faster than inserting 8 elements one-by-one)
  26.          ----------------------------------------------------------------
  27.       end
  28.    end
  29. end
  30.  
  31. local time_start = os.clock()
  32. for j = 1, 1e8 do
  33.    insert(nil, 1)
  34. end
  35. print(os.clock() - time_start)
Advertisement
Add Comment
Please, Sign In to add comment