Oh yes. Thanks.
TLDR; those are fixed. Should pygame.Vector2 and Vector2 go in pygame.locals by default?
a) I fixed it so it returns a Vector2, or Vector3 as is appropriate.
Something like v.xyxy (length==4) returns a tuple.
type(v.xy) == float
type(v.xy) == Vector2
type(
v.xyz) == Vector3
type(v.xyzx) == tuple
b) v.x = 3 works again.
c) updated the docs, removed experimental notice.
d) swizzling is now faster than fetching single attributes. (
v.xyz is faster than (v.x, v.y, v.z)).
In [1]: import pygame.math
In [2]: v = pygame.math.Vector3(1,2,3)
In [3]: %timeit v.xyz
The slowest run took 230.33 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 77.8 ns per loop
In [4]: %timeit pygame.math.Vector3(v.x, v.y, v.z)
The slowest run took 25.69 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 465 ns per loop
In [5]: %timeit v.xy
The slowest run took 25.73 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 73.5 ns per loop
In [6]: %timeit (v.x, v.y)
The slowest run took 18.30 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 135 ns per loop
In [7]: %timeit pygame.math.Vector2(v.x, v.y)
The slowest run took 31.03 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 353 ns per loop
In [8]: Vector2 = pygame.math.Vector2
In [9]: %timeit Vector2(v.x, v.y)
The slowest run took 13.55 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 310 ns per loop
This was because before it was handling swizzle attribute access by waiting for an error.
Now, (x,y,z) attribute access is just as fast, and swizzles are a lot faster.
However other methods are about 10ns slower (on this machine).
In [10]: pygame.math.disable_swizzling()
In [11]: %timeit v.is_normalized()
The slowest run took 28.18 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 83.3 ns per loop
In [12]: pygame.math.enable_swizzling()
In [13]: %timeit v.is_normalized()
The slowest run took 28.89 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 94.1 ns per loop
Another downside of Vectors at the moment is that they allocate more slowly than tuples.
Because python tuples and lists are optimized over normal objects, and use a memory pool.
However, it's close to list if you use the list() constructor, and 2x faster than a python object.
In [1]: import pygame.math
In [2]: v=pygame.math.Vector2(1,2)
In [3]: Vector2 = pygame.math.Vector2
In [4]: %timeit Vector2(1.0,2.0)
The slowest run took 11.90 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 197 ns per loop
In [5]: %timeit (1.0,2.0)
100000000 loops, best of 3: 14.5 ns per loop
In [6]: %timeit list((1,2))
The slowest run took 5.76 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 205 ns per loop
In [7]: class V(object):
...: def __init__(self, x, y):
...: self.x = x
...: self.y = y
...:
In [8]: %timeit V(1,2)
The slowest run took 5.96 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 422 ns per loop
What about?
1) Putting them in pygame.Vector2, pygame.Vector3 and in pygame.locals? pygame.math imported by default.