Tjakka5

Untitled

Aug 23rd, 2018
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.23 KB | None | 0 0
  1. -- Classes
  2. local Person_Class = {}
  3. Person_Class.__index = Person_Class
  4. function Person_Class:live()
  5.    self.happiness = self.happiness - 10
  6. end
  7.  
  8. local Employee_Class = setmetatable({}, Person_Class)
  9. Employee_Class.__index = Employee_Class
  10. function Employee_Class:payTaxes()
  11.    self.money = self.money - 10
  12. end
  13.  
  14. local Programmer_Class = setmetatable({}, Employee_Class)
  15. Programmer_Class.__index = Programmer_Class
  16. function Programmer_Class:program()
  17.    self.frustration = self.frustration + 10
  18. end
  19.  
  20. -- Components
  21. local Person_Component = {}
  22. Person_Component.objects = {}
  23. function Person_Component.add(o)
  24.    Person_Component.objects[#Person_Component.objects + 1] = o
  25. end
  26. function Person_Component.update()
  27.    for i = 1, #Person_Component.objects do
  28.       local o = Person_Component.objects[i]
  29.       o.happiness = o.happiness - 10
  30.    end
  31. end
  32.  
  33. local Employee_Component = {}
  34. Employee_Component.objects = {}
  35. function Employee_Component.add(o)
  36.    Employee_Component.objects[#Employee_Component.objects + 1] = o
  37. end
  38. function Employee_Component.update()
  39.    for i = 1, #Employee_Component.objects do
  40.       local o = Employee_Component.objects[i]
  41.       o.money = o.money - 10
  42.    end
  43. end
  44.  
  45. local Programmer_Component = {}
  46. Programmer_Component.objects = {}
  47. function Programmer_Component.add(o)
  48.    Programmer_Component.objects[#Programmer_Component.objects + 1] = o
  49. end
  50. function Programmer_Component.update()
  51.    for i = 1, #Programmer_Component.objects do
  52.       local o = Programmer_Component.objects[i]
  53.       o.frustration = o.frustration + 10
  54.    end
  55. end
  56.  
  57. -- Initial creation
  58. local objects = {}
  59. for i = 1, 500000 do
  60.    local o = {
  61.       happiness   = 100,
  62.       money       = 0,
  63.       frustration = 0,
  64.    }
  65.  
  66.    objects[i] = setmetatable(o, Programmer_Class)
  67.  
  68.    Person_Component.add(o)
  69.    Employee_Component.add(o)
  70.    Programmer_Component.add(o)
  71. end
  72.  
  73. -- Actual test
  74. local s1 = love.timer.getTime()
  75. for i = 1, #objects do
  76.    local o = objects[i]
  77.    o:live()
  78.    o:payTaxes()
  79.    o:program()
  80. end
  81. local e1 = love.timer.getTime()
  82.  
  83. local s2 = love.timer.getTime()
  84. Person_Component.update()
  85. Employee_Component.update()
  86. Programmer_Component.update()
  87. local e2 = love.timer.getTime()
  88.  
  89. print(e1 - s1)
  90. print(e2 - s2)
Advertisement
Add Comment
Please, Sign In to add comment