0

I am working on a script someone created for modifying 3D digital models that was written in Python code. The original author compiles the file into a Windows executable before distributing it. I'm guessing he uses py2exe or some similar tool.

My question is, is there any speed benefit in doing so? The script is very slow, and I'm hoping for better performance after compiling the script. Thanks.

posfan12
  • 2,541
  • 8
  • 35
  • 57
  • 1
    Try it and [time it](https://docs.python.org/3/library/timeit.html) – wwii Jan 25 '18 at 18:47
  • Depending on the code, you could probaly compile parts of, or the whole code with numba. If there are only numpy arrays with eccesiive looping involved numba would be a good alternative. For example take a look here https://stackoverflow.com/questions/45399851/improving-python-code-in-monte-carlo-simulation/45403017#45403017 – max9111 Jan 26 '18 at 12:34

1 Answers1

3

No. py2exe and similar tools just create a bundle including the Python interpreter, the bytecode of your Python sources and their dependencies. It's just a deploy convenience, there's no speed advantage (besides skipping the initial parsing of the .py files; in this respect, it's like running your code the second time when the .pyc files are already created).

For "out of the box" performance improvement you can try running your script with PyPy instead of CPython - for "all interpreted" (=> no numpy & co.) numerical Python code I saw very often 20x speedups.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • Can I use PyPy to compile scripts into executables? Will they run faster than regular compiled Python scripts? – posfan12 Jan 26 '18 at 00:58
  • 1
    @posfan12: PyPy is a JIT compiler (it compiles to machine code at runtime after examining the "typical" code paths of the interpreted code), so by itself it cannot generate a "classical", ahead-of-time compiled binary. As for the "exe bundle" trick generally used for CPython by py2exe e PyInstaller, AFAIK they don't work with PyPy. Most probably your script *will* run faster, but there's no simple way to deploy it without explicitly installing PyPy as well. – Matteo Italia Jan 26 '18 at 01:03
  • Can I install regular Python and PyPy on the same machine at the same time? Are scripts called in a different way on the command line? – posfan12 Jan 26 '18 at 02:52
  • 1
    Yes. No (well, you have to write `pypy` instead of `python`, but that's about it). – Matteo Italia Jan 26 '18 at 06:37
  • I will probably just switch to another (compiled) programming language. – posfan12 Jan 26 '18 at 08:32
  • PyPy is much faster! It may be fast enough that I won't really need to rewrite it in another language. Thanks for the suggestion!! – posfan12 Jan 26 '18 at 09:58