mathutils.Vector copy() vs create new instance - the speed difference

Hello, guys.
I guess you all know the mathutils.Vector component aviable in BGE Python. And some of you may know that if you need to get an instance of one and the same size vector(e.g. zero vector), there are 2 ways:


vector = mathutils.Vector([0.0, 0.0, 0.0])
newVector1 = vector.copy()
newVector2 = vector.copy()
newVector3 = vector.copy()

or


newVector1 = mathutils.Vector([0.0, 0.0, 0.0])
newVector2 = mathutils.Vector([0.0, 0.0, 0.0])
newVector3 = mathutils.Vector([0.0, 0.0, 0.0])

And now I am stuck here wondering - which method is faster? I actually don’t know a good way to test it so I am asking you - what do you think/know is the fastest way? Or maybe there is even better way to initialize multiple vectors with identical values?

After some quick testing, it appears that creating a new vector is about 50% slower than just copying an existing vector. I suspect this is because copy does not have to bother with parsing the Python objects you use to create a new vector.

Note that while copying is a bit faster, neither operation takes a significant amount of time unless performed in very large numbers (e.g. making more than 10,000 vectors), so you probably shouldn’t be worrying about performance regardless.

I think* the speed difference came from the new list for every instances, try using one copy of it


vec = (0,0,0)  # tuple works too
new1,new2,new3 = (mathutils.Vector(vec) for v in range(3))  # generator comprehension is slow, only for demo

regardless, in python I’ll be more concern of LOOPS than a few instances

OK! Thanks for report, guys. So it really doesn’t matter when it’s a deal of around 20 vectors, but a several thousands would be better if not created as a new vector how I shown it. Thanks for info:)