The Smart C compiler may optimize your loop by knowing that at the end of a there will always be 1. Python cannot do this because when __next__ with xrange it needs to call __next__ on the xrange object until it raises the StopIteration value. python cannot know if __next__ have a side effect until it calls it, so there is no way to optimize the loop. The message from this paragraph is that MUCH HARDER optimizes the Python compiler than the C compiler, because python is such a dynamic language and requires the compiler to know how the object will behave in certain circumstances. In C, this is much simpler, because C knows exactly what type of each object is ahead of time.
Of course, the compiler aside, python should do a lot more work. In C you work with basic types using the operations supported in hardware instructions . In python, an interpreter interprets bytecode one line at a time in software . Obviously, this will take longer than machine-level instructions. And the data model (for example, calling __next__ again and again) can also lead to many function calls that C doesn't need to do. Of course, python does this to make it much more flexible than you can in a compiled language.
A typical way to speed up python code is to use libraries or native functions that provide a high-level interface for low-level compiled code. scipy and numpy are great examples of such a library. Other things you can learn are using pypy , which includes the JIT compiler - you probably won't reach your own speeds, but it will probably beat Cpython (the most common implementation) or write extensions to C / fortran using the Cpython API, cython or f2py for critical sections of code.
source share