Why is the trivial loop in python running much slower than in C ++? And how to optimize it?

just run the almost empty for loop in python and in C ++ (as shown below), the speed is very different, 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 python speed?

(Addition: I made a bad example here in the first version of this question, I do not mean that a = 1 so that the C / C ++ compiler can optimize this, I mean that the loop itself consumed a lot of resources (maybe I should use + = 1 as an example.) And what I mean by how to optimize is that if a for loop is similar to a simple + = 1, how can it run at the same speed as C / C ++ ? In my practice, I used Numpy, so I can no longer use pypy (at the moment), are there some general methods for creating a loop on much faster (for example, the generator in the generation list)?)

+4
source share
4 answers

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.

+11
source

Just because Python is a higher-level language and needs to do more different things at each iteration (e.g. acquiring locks, resolving variables, etc.).

“How to optimize” is a very vague question. There is no “general” way to optimize any Python program (everything possible has already been done by Python developers). Your specific example can be optimized as follows:

 a = 1 

Which, incidentally, will be done by any C compiler.

If your program works with numeric data, then using numpy and its vectorized routines often give you a big performance boost, since it does everything in pure C (using C-loops, not Python) and should not take the interpreter lock and all that.

+5
source

As you go more abstractly, speed will decrease. The fastest code is the assembly code, which is written directly.

Read this question. Why are Python programs often slower than an equivalent program written in C or C ++?

0
source

Python is an (usually) interpreted language, meaning that the script should be read in turn at runtime, and its commands are compiled into useful bytecode at this point.

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

For this reason, Python will never be as fast as C.

Edit: in fact, python compiles the INTO C code at runtime, so you get these .pyc files.

-1
source

Source: https://habr.com/ru/post/1484221/


All Articles