Game Development Community

is cloning faster the creating new t2dStaticSprite or any other object?

by Eyal Erez · in iTorque 2D · 02/20/2009 (3:29 pm) · 3 replies

Just wondering in terms of performance what makes more sense.
I generate a lot of objects on the fly and I think I've read somewhere that cloneWithBehavior() should be faster.

#1
02/20/2009 (3:55 pm)
I would think not, but I haven't done any formal test.

Here's what I've found by looking through the source.

When creating a a new object in script this is called
ConsoleObject *object = ConsoleObject::create(callArgv[1]);

cloneWithBehaviors calls these two, among others:
t2dSceneObject* newObject = static_cast<t2dSceneObject*>(ConsoleObject::create(className));
copyTo(newObject);

The first two are mostly the same thing, just alloc()ing a new instance of the bast class, but copyTo() probably takes a bit of time, since it copies a number of fields (c++ variables), and then all the behaviors.

I don't thing copyTo() would take too long, but obviously it is longer than not calling it.

However, if you are going to copy all the behaviors and fields anyway, it's certainly going to be faster than doing it in script. I would think(again, no real testing here) that it would be faster to use copyTo() to copy all the fields and behaviors, than to copy 5 or 6 fields and the behaviors through script.
#2
02/20/2009 (4:18 pm)
I found this to be an interesting question, so I wrote up a unit test for it (found here)

The results were not very surprising really, except for one thing. Adding objects to the scenegraph takes longer than actually creating the objects themselves!

Anyway, here are the results:
Setup Time Taken: 15
Clone Time Taken: 1279
Clone Time Taken: 1046

It is by no way definitive, but it looks as if the first method (clone(), which is cloneWithBehaviors()) takes about 20% longer to run than regular clone.
#3
02/20/2009 (4:43 pm)
--edit---
I just did a little test. not very scientific so I don't have any numbers to share.
I cloned a shit load of objects in a loop vs creating them using new t2d...
The cloning is way faster.