Why does trivial loop in python run so much slower than the same in C++? And how to optimize that? [duplicate]

风格不统一 提交于 2020-01-11 05:34:10

问题


simply run a near empty for loop in python and in C++ (as following), the speed are very different, the python is more than a hundred times slower.

a = 0
for i in xrange(large_const):
  a += 1
int a = 0;
for (int i = 0; i < large_const; i++)
  a += 1;

Plus, what can I do to optimize the speed of python?

(Addition: I made a bad example here in the first version of this question, I don't really mean that a=1 so that C/C++ compiler could optimize that, I mean the loop itself consumed a lot of resource (maybe I should use a+=1 as example).. And what I mean by how to optimize is that if the for loop is just like a += 1 that simple, how could it be run in the similar speed as C/C++? In my practice, I used Numpy so I can't use pypy anymore (for now), is there some general methods for making loop far more quickly (such as generator in generating list)? )


回答1:


A smart C compiler can probably optimize your loop away by recognizing that at the end, a will always be 1. Python can't do that because when iterating over xrange, it needs to call __next__ on the xrange object until it raises StopIteration. python can't know if __next__ will have side-effect until it calls it, so there is no way to optimize the loop away. The take-away message from this paragraph is that it is MUCH HARDER to optimize a Python "compiler" than a 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, that's much easier because C knows exactly what type every object is ahead of time.

Of course, compiler aside, python needs to do a lot more work. In C, you're working with base types using operations supported in hardware instructions. In python, the interpreter is interpreting the byte-code one line at a time in software. Clearly that is going to take longer than machine level instructions. And the data model (e.g. calling __next__ over and over again) can also lead to a lot of function calls which the C doesn't need to do. Of course, python does this stuff to make it much more flexible than you can have in a compiled language.

The typical way to speed up python code is to use libraries or intrinsic functions which provide a high level interface to low-level compiled code. scipy and numpy are excellent examples this kind of library. Other things you can look into are using pypy which includes a JIT compiler -- you probably won't reach native speeds, but it'll probably beat Cpython (the most common implementation), or writing extensions in C/fortran using the Cpython-API, cython or f2py for performance critical sections of code.




回答2:


Simply because Python is a more high level language and has to do more different things on every iteration (like acquiring locks, resolving variables etc.)

“How to optimise” is a very vague question. There is no “general” way to optimise any Python program (everythng possible was already done by the developers of Python). Your particular example can be optimsed this way:

a = 1

That's what any C compiler will do, by the way.

If your program works with numeric data, then using numpy and its vectorised routines often gives you a great performance boost, as it does everything in pure C (using C loops, not Python ones) and doesn't have to take interpreter lock and all this stuff.




回答3:


Python is (usually) an interpreted language, meaning that the script has to be read line-by-line at runtime and its instructions compiled into usable bytecode at that point.

C is (usually) a compiled language, so by the time you're running it you're working with pure machine code.

Python will never be as fast as C, for that reason.

Edit: In fact, python compiles INTO C code at run time, that's why you get those .pyc files.




回答4:


As you go more abstract the speed will go down. The fastest code is assembly code which is written directly.

Read this question Why are Python Programs often slower than the Equivalent Program Written in C or C++?



来源:https://stackoverflow.com/questions/16899332/why-does-trivial-loop-in-python-run-so-much-slower-than-the-same-in-c-and-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!