Hi,

I'm trying to compare different python-C wrapping techniques to see which would be faster and also more suited to game development. I'm using Stackless Python 2.6.2 and boost 1.39.0. I implemented a simple Vec3 (3D vector) class in C++ and wrapped it with boost:python. All it does is multiplications and additions, so it implements just two operators. The thing is, it proves to be kind of slow compared to Cython/Pyrex code that does the same. I think it should run faster than Cython code.
(Note: Cython is not an abbreviation for C/Python API)
I compiled the python library from boost, in release mode, and then linked the vec3 module, provided below, to the compiled boost_python library. (I used -O2 when compiling the vec3 module)

The testing goes like this: each "tick", 10000 objects update their position, according to their velocity and timedelta since last "tick",
and I'm measuring the average time a tick takes to complete.
On my machine doing this with Cython takes ~0.026 sec/tick, while doing it with boost.python takes like 0.052 sec/tick (The overhead introduced by python's iterating through the list of objects each tick is about 0.01 sec) During one tick, for each object, python runs this: "self.position += self.velocity * time_delta",
where position and velocity are instances of Vec3.

I was hoping for results better than with Cython, by using Boost. Am I doing something wrong?



Source code:
vec3.cpp
==========
#include <boost/python.hpp>
using namespace boost::python;

class Vec3 {

   float x, y, z;

public:
   Vec3(float x, float y, float z);
Vec3 &operator*=(float scalar);
   Vec3 operator*(float scalar) const;
   Vec3 &operator+=(const Vec3 &who);
// that `const Vec3` is REALLY needed, unless you want error monsoon to come down
};

// === boost:python wrapper ===
// publish just += and * to python

BOOST_PYTHON_MODULE(vec3)
{
   class_<Vec3>("Vec3", init<float, float, float>())
       .def(self += self)
       .def(self * float())
   ;
}

// === implementation ===

Vec3::Vec3(float x, float y, float z) {
   this->x = x;
   this->y = y;
   this->z = z;
}

Vec3 & Vec3::operator*=(float scalar) {
   this->x *= scalar;
   this->y *= scalar;
   thiz->z *= scalar;a
}

Vec3 Vec3::operator*(float scalar) const {
   return Vec3(*this) *= scalar;
}

Vec3 & Vec3::operator+=(const Vec3 &who) {
   this->x += who.x;
   this->y += who.y;
   this->z += who.z;
   return *this;
}
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to