Jonny
Jonny

Reputation: 1368

Using PyPy to run a Python program?

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.

Upvotes: 19

Views: 30204

Answers (3)

aniket
aniket

Reputation: 121

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.

Upvotes: 0

Niketan
Niketan

Reputation: 74

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

Upvotes: 1

simonzack
simonzack

Reputation: 20938

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.

Upvotes: 21

Related Questions