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

Re: [pygame] 2D vector class



Ethan Glasser-Camp wrote:
I'm attaching the modified benchmarking script.
[...]
def ScreenTranslationTestVec2d(loop_count):
for i in xrange(loop_count):
final_pos = (TestData.object_position_tuple - TestData.screen_offset_tuple)*TestData.screen_scale
def ScreenTranslationTestVec2dTuple(loop_count):
for i in xrange(loop_count):
final_pos = (TestData.object_position_tuple - TestData.screen_offset_tuple)*TestData.screen_scale

Shouldn't the first function use the variables without the "_tuple" suffix?

By the way, just out of curiosity I tested the vector class (http://cgkit.sourceforge.net/doc2/vec3.html) in my cgkit package (http://cgkit.sourceforge.net):

<function ScreenTranslationTestVec2d at 0x009E3630> runs at 40641.4 loops/s
that's 677.36 loops to fill 60 fps
or 33.87 at a 5.0% budget at 60 fps

<function ScreenTranslationTestVec2dTuple at 0x009E30F0> runs at 47596.5 loops/s
that's 793.27 loops to fill 60 fps
or 39.66 at a 5.0% budget at 60 fps


<function ScreenTranslationTestExplicit at 0x009E33B0> runs at 700429.5 loops/s
that's 11673.83 loops to fill 60 fps
or 583.69 at a 5.0% budget at 60 fps


<function ScreenTranslationTestVec3 at 0x009E3230> runs at 171071.2 loops/s
that's 2851.19 loops to fill 60 fps
or 142.56 at a 5.0% budget at 60 fps

It's actually a 3d vector but as it is implemented in C++ it is still more than 3 times faster than the Vec2d or tuple version (at least using your test setup which also contains those 3 attribute lookups inside the loop). I don't have a true vec2 class yet but was already considering to add one in the future.

Greg Ewing wrote:
Doing vector operations one at a time in Python is
always going to be either slow or very slow, no matter
how you go about it.

IMO, the best way to make this sort of thing fast
is to arrange things so that you can use Numeric to
operate on multiple vectors at once. For instance,
store all your object positions in one array and
all the velocities in another, and then just add
the two arrays together.

I agree with that (at least as long as the number of objects is higher than a certain (small) threshold).


I used something like this very effectively in a
recent project where I wanted to find the intersection
of a ray with a terrain mesh.

I hope you'll apologize if I take this opportunity to mention cgkit once more. ;) There is already a ray-triangle mesh intersection method inside cgkit. This method is implemented in C/C++ and is based on the paper "Fast, minimum storage ray-triangle intersection" by Tomas Möller and Ben Trumbore. See here: http://cgkit.sourceforge.net/doc2/node152.html


- Matthias -