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

Re: [pygame] Re: removing 'experimental' notice from pygame.math



Some static constructors could be useful.

up, down, left, right
forward, back
zero, one, two, ...

>>> velocity = Vector3(1, 2, 3)

We want to go two left? This is sort of nice.
>>> velocity + Vector3.left * 2
<Vector3(-1, 2, 3)>

>>> Vector3.left
<Vector3(-1, 0, 0)>

>>> Vector3.right
<Vector3(1, 0, 0)>

>>> Vector3.up
<Vector3(0, 1, 0)>

They are unit vectors (length 1) in a particular direction.

Plus a few other useful constants.
>>> Vector3.one
<Vector3(1, 1, 1)>

etc.



​If I remove PyArg_ParseTuple entirely...
%timeit Vector3()
10000000 loops, best of 3: 104 ns per loop

(I guess it'd be around 84ns as a static attribute lookup which constructs a new vector).

So these special constructors, as well as being readable would be faster.

It'd still be quicker to declare them as variables, and make them immutable.
vec3_left = Vector3(-1, 0, 0)
%timeit vec3_left
10000000 loops, best of 3: 24 ns per loop


Note below, how the 'vec3_left' variable is lots quicker.

%timeit velocity + Vector3(-1, 0, 0) * 2
1000000 loops, best of 3: 451 ns per loop

%timeit velocity + vec3_left * 2
10000000 loops, best of 3: 184 ns per loop

We make a pretend Vector3 with a left variable to see how quick it is.
In [27]: class Vector3:
    ...:     pass
    ...:

In [28]: Vector3.left = vec3_left

%timeit Vector3.vector_left
10000000 loops, best of 3: 44.1 ns per loop

In [29]: %timeit velocity + Vector3.left * 2
1000000 loops, best of 3: 214 ns per loop

So it seems making some immutable vectors would be useful for a 10.2x - 18.8x speedup.
Without immutable vectors, 5.3x speedup.



Plus, I think they'd be more readable, and perhaps instructive.