8

Is there a difference in python programming while using just python and while using pypy compiler? I wanted to try using pypy so that my program execution time becomes faster. Does all the syntax that work in python works in pypy too? If there is no difference, can you tell me how can i install pypy on debian lunux and some usage examples on pypy? Google does not contain much info on pypy other than its description.

Justin Carrey
  • 3,563
  • 8
  • 32
  • 45

2 Answers2

13

From the pypy features page:

PyPy 1.9 implements Python 2.7.2 and runs on Intel x86 (IA-32) and x86_64 platforms, with ARM and PPC being underway. It supports all of the core language, passing the Python test suite.

This means that pretty much any code that you've written in Python 2.7 will work. The only exceptions worth mentioning are some python extensions written in C, such as numpy.

Installation should be fairly easy, you can download a linux binary from here. Then simply extract the interpreter. From this point, you can run your python programs similar to how you would run them with the normal python interpreter.

At the command line, instead of:

python my_program.py

Use:

path/to/where/you/installed/pypy my_program.py

For examples of how/why you might want to use pypy, check out this video from PyCon 2012.

Wilduck
  • 13,822
  • 10
  • 58
  • 90
  • 2
    Also see http://pypy.org/compat.html which lists all of the major known differences between PyPy and the current 2.x CPython, and has a link to all of the known differences down to a niggling level of detail. – abarnert Sep 14 '12 at 21:35
  • isn't pypy faster in general? When i execute my program using pypy, it is taking twice the time than normal execution of a python script. weird...!! – Justin Carrey Sep 14 '12 at 21:42
  • 3
    Pypy will be faster in many cases, but not all. You'll be more likely to see larger speedups in larger, longer executing programs. There's a speed comparison between CPython and PyPy at a number of different tasks here: http://speed.pypy.org/ Notice that PyPy does much better on some than others. – Wilduck Sep 14 '12 at 22:14
2

pypy is a compliant alternative implementation of the python language. This means that there are few (intentional) differences. One of the few differences is pypy does not use reference counting. This means, for instance, you have to manually close your files, they will not be automatically closed when your file variable goes out of scope as in CPython.

Hans Then
  • 10,935
  • 3
  • 32
  • 51
  • "*few* intentional syntax differences"? As opposed to none? Could you give any syntactic differences? –  Sep 14 '12 at 21:28
  • Ah sloppy of me. I meant differences, not specifically syntax differences. – Hans Then Sep 14 '12 at 21:30
  • 3
    Files *will* be automatically closed, you just can't rely on it happening immediately. So `open('foo', 'w').write('test'); print open('foo', 'r').read()` isn't likely to print "test". But that's bad Python anyway, since (a) the official Python docs don't guarantee that it will be used, and more importantly (b) relying on a side effect triggered by things going out of scope to have a meaningful impact on the way your program operates is a *gross* violation of "explicit is better than implicit". – Ben Sep 16 '12 at 02:21