Apr-08-2019, 02:27 AM
(Apr-04-2019, 12:47 PM)comotai Wrote: What is wrong with my code? Why is the performance so slow compared to C doing exactly the same thing?
If we are talking about CPython (standard Python), implementation of Python programming language in C,
execution model of a python program (script) is significantly differed from that is being written in C language. C-program is compiled into machine code and than executed. Python program is compiled into bytecode that is further interpreted/executed by CPython engine. That is why Python program is executed slower than C one. Sometimes one might use a just-in-time (JIT) compiler that compiles bytecode into machine code and runs it (e.g. PyPy). This usually leads to increasing of execution speed. In you case, you are likely never achieve comparable to C-language execution speed using CPython (or ever PyPy, Cython, or something else). You program is already written in C, and probably optimized. If you ever used Cython, that translates Python program into C-code, that is further compiled into executable, the program wouldn't be so fast, as it would natively written in C. The only way of improving performance in your case (if program isn't too complex) is to rewrite it using assembly language (or may be apply some optimization keys at compilation time, -O2 etc.). So, why do you need to rewrite a program (originally written in C) in Python?!
You can try to achieve comparable to C (but liekly slower, than you already have, I think) performance using NumPy package.
import numpy as np data = memoryview(b'byte') # point to your shared memory instead... result = np.ndarray(buffer=data, shape=(2,), dtype=np.int16) # change shape and dtype to interpret shared memory as array properly # result is data interpreted as two 2-byte integers