[TEST] Cython performances test

This is cython : http://cython.org/

So cython compile python scripts into .cpp or .c files and these files can be further compiled into .pyd(windows) or .o(linux) . The .pyd and the .o files can be directly imported into python (like any normal python module). It also allows static typing.

The thing is that running cython module gives a 35% speedup if the python script used dynamic typing but it can goes up to a 400% speedup if the script used static typing . Source : http://docs.cython.org/src/quickstart/cythonize.html

This is the performance test I’ve created:

The test consists of a blend file with 3 scenes and 3 modules.

The 3 modules:

  • module.py : A normal python module
  • module_cd.pyx : A normal python module but compiled in cython
  • module_cs.pyx : A cython module using static typing

The 3 scenes:

  • CYTHON_Module(Dynamic) for module_cd
  • CYTHON_Module(Static) for module_cs
  • PYTHON_Module for module.py

This is the script:

def f():
    x = 5
    return x**2-x

Static typing:

cpdef int f():
    cdef int x = 5
    return x**2-x

def itegrate_f():
    cdef int rep
    rep = f()

    return rep

By pressing the space bar, an empty spawn 10 other empties that execute the module used at every logic tic.
A property “Num” indicates the number of empties that have been spawned.

This is the results that I got. I am really impressed:

CPU: Intel Core i7 M620 @ 2.67 GHZ (2 core) using Windows 7 ultimate.

Test :
Number of objects before logic profile reach about 50%
Python module : 4420 objects
Dynamic cython module : 5030 objects
Static cython module: 11740 objects (for some reasons it reached 25000 objects 2 times on the 10 tests)

If you want to try the test, download the attachement. You will have to compile the 2 .pyx using cython first (be sure to have the right version). It would be awesome if you could post the results too.


release.zip (67.4 KB)

Firstly, yes, of course it is. The Blender Game Engine runs Python as any other application does.

Well I’m testing if it have good impact on the logic.

It took me 30 minutes to figure why I coudn’t import the compiled script in blender : I was compiling the script in python x86 while I was using a 64 bits version of python (blender x64). :frowning:

So far the results are good.

Will this give you more fps when the game is running?

If your game use alot of logic then yes.

With the python module and 12000 objects I was running at 5 fps but with the static cython it was still at a solid 60 fps.

It’s good that you’re investigating this. I’ve been tempted to before, but I don’t want to have a lot of mixed code. It is best for me that I implement this towards the end of my development I think. If you wanted to post some tutorials for new users, that would make this more of a resource.

WOW!! Thats a huge difference…I have tested the blend and I must say that I am impressed. Way(i mean ,mega way) more faster.

I will try it in a few days.
But your benchmarks sounds really good.

It would be cool, if it is possible, to implement it into blender ge directly.

It is possible to install cython directly into blender and compile the .pyx on runtime by using pyximport.

import pyximport; pyximport.install()
#Now you can import .pyx files as if they where python module
import cythonModule

I never tested it .


I just discovered that Visual c++ 2010 express or mingW (mingw do not work very well) is REQUIRED to compile .pxd on windows therefore using the method stated above is far from being a good idea.

You don’t need to install this into Blender. It is a Python extension, and it is designed for that very purpose.

Python can import .pyd or .o files without cython installed BUT to compile .pyx files you need cython installed.

hey… just started your testfile.
the bge has problems to import the .pyx modules… So cython needs to be installed to test the example?

i’m forgot to compile the [module_cd.pyx] [module_cs.pyx] .pyx using cython.

Awesome! I had no idea this was possible. Does using Cython introduce any limitations as far as what you can do with BGE scripts? Or is it functionally identical to regular Python?

did it run out of the box for you? or have you set up something to run it properly?
I have problems to get this running…
I would love to investigate some time into this topic! I think this could be a great method to speed up a particle system alot!

I’d like to try it… but, I have no idea on how to test this… as I’m using built-in python

you must installing Cython http://docs.cython.org/src/quickstart/install.html
For Windows there is also an executable installer available for download.
[EPD] http://www.enthought.com/products/epd.php
[Pythonxy] http://www.pythonxy.com/
[Sage] http://sagemath.org

and compile the .pyx file using cython http://docs.cython.org/src/reference/compilation.html

Like blendx said.
There’s also http://www.lfd.uci.edu/~gohlke/pythonlibs/#cython if you just want an executable.

Cython have some limitations when working with blender: it seems that importing blender built-in module (ie: bge) from the cython file can cause instabilities and if you import the module another time; it crashes blender. On windows at least.

So if you can’t import bge, does that mean you can’t work directly with game objects, since KX_GameObject resides in the bge module? Then you’d be limited to writing utility modules.

If the problem would only be with directly importing the BGE module, you could still pass data around (i.e. pass collections of game objects or other resources).