Cython: 26% faster on a simple benchmark. No code change. #1583
Replies: 7 comments
-
@rchateauneu Is your 20% number based on a generic benchmark or did you try it with rdflib? |
Beta Was this translation helpful? Give feedback.
-
This is a generic benchmark that I confirmed on other projects, but not on rdflib yet. It is possible to optimize only some files and leave the others intact. This makes the installation faster (Because less files have to be compiled/linked). The nice thing is that it is completely transparent: Cython transforms a Python file into a C file which is after that compiled and linked into a pyd or a so file, and is imported as is by normal Python mechanism. In "Pure Python" mode - which is the less intrusive, the variables types can be specified with decorators (https://cython.readthedocs.io/en/latest/src/tutorial/pure.html#magic-attributes) or with a pxd auxiliary file. In this case, there in no source change at all. |
Beta Was this translation helpful? Give feedback.
-
I have done some benchmarking and profiling of rdflib in the past, there are not many tight loops to optimize though. (at least in the sparql focused use cases i benchmarked) Still I might be interested in seeing if rdflib profits of this. Are you planing to do some testing with rdflib and cython? |
Beta Was this translation helpful? Give feedback.
-
I created a fork, and try to see what is possible. You can generate the pyd file along the pure Python one, with: ... and remove it with: Do you please have a specific and simple benchmark I could focus on, please ? A command without too many dependencies, for example ? |
Beta Was this translation helpful? Give feedback.
-
With this benchmark and more "cythonized" files (Please see setup.py in branch): Best time interpreted: Best time compiled: Of course, it is possible to go further:
This benchmark is a simple list of calls to rdflib, nothing complicated. |
Beta Was this translation helpful? Give feedback.
-
@rchateauneu would you be willing to try out if cython can improve the parsers? #1261 |
Beta Was this translation helpful? Give feedback.
-
@white-gecko py -3.6 import.py english-wordnet-2020.ttl turtle Interpreted. Then run: 2021-02-25 18:54:05 The script is: |
Beta Was this translation helpful? Give feedback.
-
Cython is able to speedup pure Python code of about 20% apparently, without code change.
The only needed adaptation is to add some statements to setup.py which, if cython is installed could convert some or all Python source files to C, the compile them.
In a second stage, it would also be possible to add Cython decorators for static typing in the Python file. These decorators are ignored by the interpreter.
Beta Was this translation helpful? Give feedback.
All reactions