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.
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).
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.
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 .
Edit:
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.
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?
@blendx
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!
@laser_blaster:
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).