Lupus590

simple lua objects speed test

Jan 9th, 2020
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.36 KB | None | 0 0
  1.  
  2. local function new(prototype)
  3.   return setmetatable({}, {__index = prototype})
  4. end
  5.  
  6. local function uplift(object) -- TODO: performance test vs unuplifted object
  7.   local prototype = getmetatable(object).__index
  8.   local uplifitedObject = {}
  9.   for k, v in pairs(prototype) do
  10.     if type(v) == "function" then
  11.       uplifitedObject[k] = function(...)
  12.         return v(uplifitedObject, ...) -- TODO: catch errors which are blamed on this function's caller and blame our caller
  13.       end
  14.     else
  15.       uplifitedObject[k] = v
  16.     end
  17.   end
  18.   for k, v in pairs(object) do
  19.     if type(v) == "function" then
  20.       uplifitedObject[k] = function(...)
  21.         return v(uplifitedObject, ...) -- TODO: catch errors which are blamed on this function's caller and blame our caller
  22.       end
  23.     else
  24.       uplifitedObject[k] = v
  25.     end
  26.   end
  27.   return uplifitedObject
  28. end
  29.  
  30. local function upliftNew(prototype)
  31.   return uplift(new(prototype))
  32. end
  33.  
  34.  
  35. local p = {
  36.   f = function() end
  37. }
  38.  
  39. local o = new(p)
  40.  
  41. local max = 10000000
  42.  
  43. local stopTime, startTime, deltaTime
  44. startTime = os.clock("utc")
  45. for i = 1, max do
  46.   o:f()
  47. end
  48. stopTime = os.clock("utc")
  49. deltaTime = stopTime - startTime
  50. print(deltaTime)
  51.  
  52. o = uplift(o)
  53. startTime = os.clock("utc")
  54. for i = 1, max do
  55.   o.f()
  56. end
  57. stopTime = os.clock("utc")
  58. deltaTime = stopTime - startTime
  59. print(deltaTime)
Advertisement
Add Comment
Please, Sign In to add comment