1

I'm creating objects from a class and want to iterate them later. Is there a more efficient way than this?

class1 = {}
class1.value1 = "1"
class1.value2 = 2
class1.tvalue1 = {}

function class1:new()
    local class = self
    local object = {}
    setmetatable(object,class)
    class.__index = class
    object.tvalue1 = {}
    table.insert(objects, object)  -- to iterate them later, stupid way I think
    return object
end
function class1:alterValue1(input)
    self.value1 = input
    return self.value1
end
function class1:alterValue2(input)
    self.value2 = input
    return self.value2
end


randomName = class1:new()
otherName  = class1:new()
weirdoName = class1:new()
.
.
.
(n)

Is there a more efficient way to iterate the objects than inserting them into some kind of objects-table in the constructor?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jawo
  • 856
  • 1
  • 8
  • 24
  • I know, thinks are named a bad way in this example, but it's only an example and you should unterstand the issue. – jawo Sep 11 '14 at 06:14
  • Why do you think the current method is not efficient? – Yu Hao Sep 11 '14 at 06:16
  • It just feels bad to do it like that,... don't know why. – jawo Sep 11 '14 at 06:18
  • okay, if this method is efficient, woulnd it be better to return "objects.object" from constuctor, not just "object", to make sure it refeers to the correct memory? – jawo Sep 11 '14 at 06:34
  • 1
    @Sempie: There is only one object: the local variable `object` and `objects[#objects]` are the same (`objects.object` is something different, probably `nil`). Please not that storing the objects in a table will prevent them from being garbage collected. If you don't want that, make `objects` a weak-keyed table, store the objects like `objects[object] = true`, and iterate via `pairs`. – siffiejoe Sep 11 '14 at 07:30

1 Answers1

1

The method you use is to insert each "instance" of class1 into a table called objects. If you want to iterate over all instances of class1, i.e. use a for loop to do something with/on each instance, then you don't have a choice, you have to store the instances in a table, as you are doing.

The next question is, does where that table exist make a difference to performance? In Lua, access of locals (and upvalues) has marginally better performance than accessing globals, but whether the difference matters depends on what you need to do with the global variable, and in the case of iteration, what you do in the body of the iteration. See for instance Optimizing Using Local Variables.

In all likelihood, the operations in the body of the iteration loop will make any difference (between your table being local vs global) insignificant. You might want to look at What can I do to increase the performance of a Lua program?, but most importantly, only optimize based on

  • profiling: using os.clock, and possibly something like PepperFish -- caveat emptor: I Have not tried it.
  • "sound design" principles: carefully choose your containers and algorithms, for instance you can get O(N^2) instead of O(N log N) if you aren't careful with your design, similarly O(N log N) instead of O(log N), or O(N) instead of O(1), etc.
Community
  • 1
  • 1
Oliver
  • 27,510
  • 9
  • 72
  • 103