I have read PyPy — How can it possibly beat CPython? and countless other things but i am not able to understand how something written in Python be faster than python itself.
The only way I can think of is that PyPy somehow bypasses C and directly compiles into assembly language instructions. If that is the case, then it is fine.
Can someone explain to me how PyPy works? I need a simple answer.
I love python and want to start contributing. PyPy looks like an awesome place to start irrespective of whether they pull my code or not. But I am not able to understand from the brief research I have done.
The easiest way to understand PyPy is to forget that it’s implemented in Python.
It actually isn’t, anyway, it’s implemented in RPython. RPython is runnable with a Python interpreter, but Python code is not able to be compiled by the RPython compiler (the PyPy translation framework). RPython is a subset of Python, but the parts that are “left out” are substantive enough that programming in RPython is very different from programming normally in Python.
So since Python code can’t be treated as RPython code, and idiomatic RPython programs “look and feel” very different to idiomatic Python programs, lets ignore the connection between altogether, and consider a made-up example.
Pretend I’ve developed a new language, Frobble, with a compiler. And I have written a Python interpreter in Frobble. I claim that my “FrobblePython” interpreter is often substantially faster than the CPython interpreter.
Does this strike you as weird or impossible? Of course not. A new Python interpreter can be either faster or slower than the CPython interpreter (or more likely, faster at some things and slower at others, by varying margins). Whether it’s faster or not will depend upon the implementation of FrobblePython, as well as the performance characteristics of code compiled by my Frobble compiler.
That’s exactly how you should think about the PyPy interpreter. The fact that the language used to implement it, RPython, happens to be able to be interpreted by a Python interpreter (with the same external results as compiling the RPython program and running it) is completely irrelevant to understanding how fast it is. All that matters is the implementation of the PyPy interpreter, and the performance characteristics of code compiled by the the RPython compiler (such as the fact that the RPython compiler can automatically add certain kinds of JITing capability to the programs it compiles).