3

I am really bad at compiling programs, and I just want to know if my python 2.5 program would be faster if I converted it to a .exe using py2exe. I don't want to spend a lot of time trying to compile it if it will just be slower in the end. My program uses OpenCV and PyAudio, but I think that are the only non pure-python modules it uses. Thanks!

NOTE: I do not think this question requires a snippit of code, but if it does, please say so in the comments. Thanks!

Tom
  • 846
  • 5
  • 18
  • 30
  • This would be very dependent on what exactly your Python program does, and can only be answered by actually testing the two side by side. As no one here can do that for you, it's pretty unlikely this question can be answered. – Ken White Dec 24 '12 at 13:49
  • There's no 'magic bullet'. You can't just do something simple and make code go faster. You have to understand what it's doing, why it's taking too long, and find ways to get around that. – Katriel Dec 24 '12 at 14:30

3 Answers3

6

No, not really. Since it's merely a wrapper it provides the necessary files needed to run your code. Using Cython could make your program run faster by being able to compile it using C.

Aleksander S
  • 368
  • 1
  • 10
  • Could you give me a download link to compile it using Cython? I've been looking for something like that for a long time! – Tom Dec 24 '12 at 13:58
  • Note that Cython applied to unmodified Python code will only give minimal speedups, as it won't be able to do much more than remove then bytecode dispatch overhead. Almost all of the resulting C code will still use the CPython runtime, be dynamically typed, use runtime type checks, operate on boxed heap-allocated objects, have GC overhead, etc. -- to get significantly more efficient code, you have to use Cython-specific syntax to get rid of those (at the expense of dynamism and portability). –  Dec 24 '12 at 14:24
  • Most of all of your python libraries that you will ever need: http://www.lfd.uci.edu/~gohlke/pythonlibs/ Also, you should look into Numba. It's similar to Cython, but it's also a little easier to use. Look at this blog "http://jakevdp.github.com/blog/2012/08/24/numba-vs-cython/" for more details. Using Numba, a single line decorator on a function can provide a 1000 times speedup than native python (but Cython is just slightly faster). – Ryan G Dec 24 '12 at 17:18
1

If you want to make your program faster, you first have to measure why it is slow. This means finding out where the program is spending its time. Use one of the available profilers for that. As you can see from the linked examples, it is not that difficult.

If you know where the program spends most of its time, you can decide wether optimization is worthwhile or even possible. If your program is e.g. already saturating your network connection, tinkering with the program won't make it faster.

If you add the slowest code from your program to your question, maybe we can help you improve it. Usually the big gains can be made by improving on the algorithms that you use. For instance if your code does the same calculations on a long list of data, it is generally easy to make it faster with e.g. multiprocessing.Pool. If you have a N-core CPU, Pool.map() can be up to N times faster than a regular map().

If that isn't sufficient, a lot of standard python code can be run on the pypy implementation, which generally faster than the cpython implementation.

Apart from the already mentioned cython, you could also use an existing extension written in C like e.g. numpy for numerical computations. Another option of course is to write a purpose-built extension in C.

Your operating system also matters. It seems python on XP is slower on file access than e.g. on Linux.

Community
  • 1
  • 1
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • Well, my program captures many screen images and saves them to files. Right now, it captures images using wx and has them saved to files in threads. – Tom Dec 24 '12 at 22:30
  • XP has functions for capturing screenshots. You can use those directly (using ctypes) instead of via wx. See: http://stackoverflow.com/questions/531684/what-is-the-best-way-to-take-screenshots-of-a-window-with-c-in-windows If that is still too slow, you can use multiple processes to do it. – Roland Smith Dec 25 '12 at 14:06
1

I spent last 2 months working on Windows Python and I must say i am not very happy. If you have any C modules(besides standard library), you'll have huge problems to get it working.

Speed is similar to Linux, but it seems a little bit slower. Please note that py2exe is not the best. I had issues with py2exe and had to use pyinstaller. It has a better debug, it worked in cases when py2exe didn't

My biggest dissapointment was exe size, I had a simple program which used lxml, suds and it was 7-8mb big...

Goranek
  • 826
  • 1
  • 10
  • 23