[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] vector type: mutable or immutable



Hi again,

I just did a quick performance test.
The first run uses an in-place operation (as could be used with mutable types) the second run on the other hand uses the equivalent expression without the in-place operator (as would be necessary if we go with immutable types).

here are the calls with the results:

>>> timeit.repeat("x += x", "from vector import Vector2d; x = Vector2d(2, 3)", repeat=5, number=10000000)
[1.4092800617218018,
 1.4093708992004395,
 1.4093561172485352,
 1.413952112197876,
 1.4092509746551514]

>>> timeit.repeat("x = x + x", "from vector import Vector2d; x = Vector2d(2, 3)", repeat=5, number=10000000)
[2.3679590225219727,
 2.3530960083007812,
 2.3615989685058594,
 2.3519301414489746,
 2.3658668994903564]

that is quite an overhead IMHO.
Of course it is quite possible you could tweak this a bit but I imagine the result would stay more or less the same.

Note that the vector type in this example was implemented as C extension.

This make me once more lean towards mutable.



Lorenz Quack wrote:
Hi,

I'm against implementing both. I think it makes what should be a rather simple class overcomplicated. Imagine the zoo of class we would have:
Vector2d, Vector2dConst, IntVector2d IntVectro2dConst, Vector3d, ...

I think we should make up our minds and then sick with one.

And I have to add that even though I started off favoring mutable vectors I then remembered also running into the subtle bugs Brian mentioned. So now I'm leaning in the immutable direction.



Brian Fisher wrote:
I guess making both would be the most pythonic - python gives you both lists and tuples...

On Fri, May 1, 2009 at 11:04 AM, Jake b <ninmonkeys@xxxxxxxxx <mailto:ninmonkeys@xxxxxxxxx>> wrote:

    Could you make both? Default to mutable, and use VectorConst ( insert
    better name ) where needed.

           .offset = Vector3Const(1,2,3)
           .vel = Vector3( offset )

    (throw exception when you try to modify a const vector?)
    --
    Jake