16

I have been told that you can use PyPy to run Python programs, which is a lot faster as it is compiled using a JIT compiler rather than interpreted.

The following program finds the largest prime factor of the number 600851475143:

import numpy as np

nr = 600851475143
n = 2

while n <= np.sqrt(nr):
    if nr%n == 0:
        nr = nr/n
    n += 1
print(nr)

What would be the procedure to run this using PyPy?

I know there is documentation on their site, but I do not understand it and would appreciate a demonstration.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
Jonny
  • 1,270
  • 5
  • 19
  • 31
  • 1
    It is not compiled in `C`. It has nothing to do with `C` in fact. It runs under a JIT. – simonzack Sep 20 '14 at 10:11
  • Note that using `math.sqrt` gives an immediate 10x speed-up under PyPy. – Veedrac Sep 20 '14 at 10:23
  • Noted (-: Do you mean math.sqrt instead of np.sqrt? – Jonny Sep 20 '14 at 10:24
  • 1
    @JackTaylor Yes. `np.sqrt` is too complicated for the JIT to inline properly. – Veedrac Sep 20 '14 at 10:26
  • 2
    Also, the JIT doesn't seem to have long enough to warm up, so it's actually slower than CPython. There's also the fact that your algorithm is particularly suboptimal; it'd be twice as fast to deal with powers of 2 and then only iterate over odd `n`. – Veedrac Sep 20 '14 at 10:43
  • I see. My program was really just an example of a short program I had lying around. Is CPython then a better way to go in general? How is this implemented? – Jonny Sep 20 '14 at 10:52
  • 2
    Nah, PyPy's way faster. You just need to let it run for long enough. For 20000 iterations, PyPy took ~0.8s for the optimized code. CPython (version 2) took 4.6s. In contrast, both Rust (`-O`) and C++ (`-O3`) took ~0.4s, so PyPy didn't even take twice the time! – Veedrac Sep 20 '14 at 10:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61597/discussion-between-jack-taylor-and-veedrac). – Jonny Sep 20 '14 at 10:58

3 Answers3

19

Add this shebang line to the top of the program:

#!/usr/bin/env pypy

If you want to do this manually, just enter pypy main.py on the command-line.

simonzack
  • 19,729
  • 13
  • 73
  • 118
  • Thank you. So there is no change in the program itself needed, you just use another program to run the file. Do you know how the speed of PyPy (JIT compiler) compares to compiled languages such as C and FORTRAN? – Jonny Sep 20 '14 at 10:16
  • They have a [live speed benchmark](http://speed.pypy.org/), it depends on the code, but as a rough estimate I'd say 2-10 times slower. – simonzack Sep 20 '14 at 10:19
  • Great. Also: when I try to run a program from the terminal using 'pypy test.py' it gives me an error, but when I add the line you suggested to the top of the program and run 'python test.py', it does work. Is this normal? Or a problem with the installation of PyPy? – Jonny Sep 20 '14 at 10:23
  • @JackTaylor That's not normal, no. That means your PyPy installation doesn't work. Note that the `#!` is meant to let you do `chmod +x test.py; ./test.py`; running `python test.py` isn't going to use PyPy. – Veedrac Sep 20 '14 at 10:24
  • I get the error 'No module named numpy', so I am guessing pypy does not see the numpy installation. Although I get no error when I add the line mentioned in the answer to the top of my program and run it normally? – Jonny Sep 20 '14 at 10:27
  • 1
    @JackTaylor PyPy has a separate set of package installations; you obviously don't have Numpy. Numpy support is somewhat experimental in PyPy, by the way, and you'll need to install it separately. – Veedrac Sep 20 '14 at 10:28
0

Keep your environment activated and go into pypyXXXX folder. Then go into bin directory and run the following commands.

pip install <packagename>

Then run your file using pypy

pypy filename.py
Niketan
  • 54
  • 3
0

for linux :

  • download the latest version of PyPy from pypy.org
  • unpack the zip doc at your fab location

for interactive session

$ ./pypy-x.y.z/bin/pypy

Python 2.7.x (xxxxxxxxxxxx, Date, Time)
[PyPy x.y.z with GCC x.y.z] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``PyPy is an exciting technology
that lets you to write fast, portable, multi-platform interpreters with less
effort

>>>>

If you want to make PyPy available system-wide, you can put a symlink to the pypy executable in /usr/local/bin. It is important to put a sylink and not move the binary there, else PyPy would not be able to find its library.

aniket
  • 111
  • 1
  • 6