Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Which is better; compiled binaries or interpreted code in python?

I'm planning to use python to build a couple of programs to be used as services, run from PHP code later on. In terms of performance, which is faster, to compile the python code into a binary file using cx_freeze or to run the python interpreter each time I run the program? Deployment environment: OS: Arch Linux ARM Hardware: Raspberry Pi [700MHz ARMv6 CPU,256MB RAM, SD Card filesystem] Python Interpreter: Python2.7 App calls frequency: High

user-image
Question ajoutée par Badr Ghatasheh , Software Engineer , bayt.com
Date de publication: 2013/07/23
THIKRALLAH SHREAH
par THIKRALLAH SHREAH , Technical Team Leader , bayt.com

Well, I'm not a python expert per say, yet I used py2exe in the past.
The intention of such applications is packaging code, from my experience with py2exe and what I have understood from it's documentation it will load the original scripts into the interpreter.
That means there is no performance difference between them in terms of performance.If you are looking for very fast performance package, always think "C", http://docs.python.org/2/extending/extending.html if you want to try that out.
I would recommend to do a bench marking for cx_freeze.
and it will be nice gesture from you to share it back here.

Tomasz Modrzejewski
par Tomasz Modrzejewski , Python Developer , Freelancer

In general, **interpreted code** is considered "better" for most Python use cases due to the following reasons: * **Flexibility and Ease of Development:** Python's interpreted nature allows for rapid development and testing. You can make changes to your code and see the results immediately without needing to go through a compilation step. This iterative process is crucial for exploratory data analysis and machine learning tasks, where experimentation is key. * **Portability:** Python's interpreted code is platform-independent. The same `.py` file can run on different operating systems without needing to be recompiled for each one. This portability simplifies deployment and collaboration. * **Debugging and Error Handling:** Python's interpreter provides detailed error messages and tracebacks that pinpoint the exact location of issues in your code. This makes debugging much easier compared to compiled languages, where errors might only surface at runtime. However, there are scenarios where **compiled binaries** might offer advantages: * **Performance:** In some cases, compiled code can execute faster than interpreted code because it's already been translated into machine instructions. However, modern Python interpreters with just-in-time (JIT) compilation can often bridge this performance gap significantly. * **Distribution:** Compiled binaries can be easier to distribute since they don't require end-users to have a Python interpreter installed. This can be useful for deploying applications to users who might not be technically savvy. * **Intellectual Property Protection:** Compiling your code into a binary can make it harder to reverse-engineer, offering some level of protection for your intellectual property. **Important Considerations:** * Tools like `cx_Freeze` and `PyInstaller` can create "compiled" Python executables, but they essentially bundle your Python code with a Python interpreter into a single package. The code itself is still interpreted at runtime. * True compilation of Python code is possible with tools like Cython or Numba, which can translate Python code into C or machine code for potentially significant performance gains. However, this often involves writing code in a more restrictive subset of Python and requires additional effort. **Conclusion:** For most Python use cases in machine learning and AI, the flexibility and ease of development offered by interpreted code outweigh the potential performance benefits of compiled binaries. However, compiled binaries might be preferable in situations where performance is critical or you need to distribute your application to non-technical users. The best choice will depend on the specific needs of your project and your priorities.

David Lee
par David Lee , Freelance developer LUA/Python , Freelancer.org

code that is portable is best. Any language that is compiled creates binaries. Some binaries fail on some machines. Having a non-compiled code like python and shell-bash scripts is good but not always fast.

Haruna Bala Magaji
par Haruna Bala Magaji , System Administrator , KEDCO

Binary it's a Machine code that can be executed directly without translation so it's easier. But for me It's better to use interpreter because it include source code and it can also bypass and execute code directly

Mustafa Rawi
par Mustafa Rawi , CEO , Cubex Solutions

Python is compiled (to machine code) at run-time every time either from source code (.py) or from byte code (.pyc or .pyo).

Python is compiled at first run into byte code in .pyc files (python compiled). If your .py files are more recent, Python will re-compile your files and replace the old .pyc files.

Python compiled files can be optimized, just a little so far, into .pyo files. To compile files in optimized format, run Python interpreter using '-o' flag without quotes.

You can double the optimization by using the flag '-oo' without quotes. In some rare cases, this may damage your code and render it unusable. This flag is not recommended unless you know exactly what you are doing.

Access rights may prevent the interpreter from generating the byte code compilation of your .py files.

Finally, and to answer your question, performance is not affected unless your .py files are too large. This is due to loading time only, not execution time.

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?