boskee / Minecraft

Simple Minecraft-inspired program using Python and Pyglet
MIT License
207 stars 33 forks source link

Cython? #33

Closed BertrandBordage closed 11 years ago

BertrandBordage commented 11 years ago

After doing some experiments using numpy (see #30), I managed to save some execution time. Now I made some experiments using Cython. And some sensible functions are executed more than 10 times faster. I therefore wonder whether we should use Cython (that mixes beautifully with numpy).

If we make an advanced terrain generator like in the original game, I bet that we will quickly see the limits of Python. And unfortunately, pyglet is not thread-safe, so it may be impossible to do parallel computations.

And of course, the main problem with Cython is that we have to compile (beautiful) code.

ghost commented 11 years ago

I think we should use Cython, but only to the most important functions. We should also keep pure Python equivalents. The game is sometimes very slow, so every performance update is needed. We can also add some command line argument to switch between Cython and Python code.

BertrandBordage commented 11 years ago

There is a nice feature of Cython called Pure Python Mode to achieve such a compatibility.

Using "magic attributes" seem painful and requires to have Cython installed, even if you don't use the compiled binaries.

But the solution of having .py files of pure Python and .pxd files where C types are defined looks like a perfect solution! I start working on this.

BertrandBordage commented 11 years ago

See #36 and #39 for the first successful implementation of Cython optimized modules for this game. There is a detailled description of the performance boost.