kaloyan suggests using a generator. Here's why that may help.
If getNodeNames()
builds a list, then your loop is basically going over the list twice: once to build it, and once when you iterate over the list.
If getNodeNames()
is a generator, then your loop doesn't ever build the list; instead of creating the item and adding it to the list, it creates the item and yields it to the caller.
Whether or not this helps is contingent on a couple of things. First, it has to be possible to implement getNodeNames()
as a generator. We don't know anything about the implementation details of that function, so it's not possible to say if that's the case. Next, the number of items you're iterating over needs to be pretty big.
Of course, none of this will have any effect at all if it turns out that the time-consuming operation in all of this is vr_world.getChild()
. That's why you need to profile your code.