Why is Python running so slowly ?, Python running
About a year ago, in Waza in 2013, Alex Gaynor mentioned a good topic: Why do programs written in Python, Ruby, and Javascript always run slowly? As he stressed, the key is that this problem has occurred. In other words, although the current language is very slow, it does not mean that there is no solution, it does not mean that the future will always be like this.
When you ask why Python is slower than the C language on the Internet, the most common answer is that Python has dynamic types. However, dynamic types do have performance impact, but this is not the main reason.
Dynamic types (the same as major programming languages like Python) make it difficult for the compiler to optimize performance. Dynamic makes every execution very different, and the compiler is difficult to optimize. However, as Alex mentioned in his talk, we spend several years studying what is the best way to perform type checks at runtime. But there is no progress.
In reality, the huge difference between C and Python is that the data structures and algorithms are different. Sometimes programmers do not notice this.
Use Python to write different codes
Let's use an instance mentioned by Alex to illustrate the problem. A Python programmer may like to use the following example to represent a point on a plane:
| 1 |
point = {'x': 0, 'y': 0} |
This method is easy to read, easy to code, and elegant in form.
On the other hand, a C-language programmer may use a struct to represent points on the plane:
| 1234 |
struct Point { int x; int y;}; |
Although this method works the same way as Python and is elegant, it is a completely different data structure. Here we tell the compiler that we have two fields x and y. Knowing the types of these two fields, the compiler will allocate a continuous memory to store the two data. In other words, it is like an array. At any time, the compiler knows where the given x and y are. We can easily access this data, just like accessing some common data.
Python uses Hash hash to solve similar problems. Therefore, the compiler cannot simply allocate continuous memory storage x and y to deal with these problems. Because these keys may appear anywhere in it. If we want to, we may also delete these keys. The compiler must use a hash function to map to any storage unit that you may point. Needless to say, these functions increase processing time. Although it may slow down a little, it can slow down your code, especially in many cases.
If you want to translate Python into C, it may be like the following:
| 123 |
std::hash_set<span> point;point[“x”] = xpoint[“y”] = y</span> |
Looking at this code snippet, it seems that the Language designers deliberately tried their best to make the hash table complex, so although it is correct, no one uses it. For this reason, people who write C language may think this is incredible, but why is it acceptable in Python?
The reason is that the "dictionaries are lightweight objects" mentality of the person who writes Python code. Look at the following code, which is the closest to the C language structure in Python:
| 1234 |
class Point(object): x, y = None, None def __init__(self, x, y): self.x, self.y = x, y |
This is useful to the compiler, just like the structure of C language. For example, the second line clearly tells the compiler that we always need at least two data segments when creating an object. We want the compiler to handle this problem.
Unfortunately, this standard Python is called CPthon and cannot be used all the time. On my machine, the following code is executed in 186 milliseconds:
| 123456 |
def sum_(points): sum_x, sum_y = 0, 0 for point in points: sum_x += point['x'] sum_y += point['y'] return sum_x, sum_y |
On my machine, replacing point ['X'] with point. x takes 201 milliseconds. That is to say, it will slow down by 8%.
In CPthon, point. x is usually processed as dict (point) ['X']. This means that the class with vertices still uses the dictionary method as before. In this case, it is easy to see why the direary ary method is regarded as "lightweight ".
Some Python code is designed for efficiency, such as PyPy, which can be executed quickly. If you use PyPy instead of Python, the execution time of the same code fragment is 21.6 and 3.75 milliseconds, respectively. This method is satisfactory compared with CPython in JIT-capable compiling. In other words, PyPy can correctly use the data structure.
I hope you can see the shortest time of 3.75 milliseconds again. This number indicates that we can perform 266000 operations in one second. These operations come from Python, where dynamic binding exists, monkey-patching (the method of extending or modifying the runtime code of dynamic languages without changing the source code. All of these use a better data structure in encoding and implementation. The next time you write a line of code in Python, think about what data structure you are using, whether it is displayed implicitly, and whether there is a better way. This is what you consider when writing a program in C language, isn't it?
Finally, I would like to believe this article is a clear example of why Python is a promising language (or a similar language ). This shows the standard Python implementation. CPython is just a reference, and it has never been designed for faster execution. As we can see today, Algorithm Implementation like PyPy can optimize your code to a good length. With the natural development of the language, these optimizations are possible. We only used Python for programming for 23 years. What would Python look like if we had 42 years of development like C?
1. Some people may argue that collections. namedtuple () is a structure closer to the C language. This is correct, but we should not complicate things too much, and our focus is effective.
2. For more information about how python works, see the python documentation.
Original article lukauskas. co. uk
Python: after the program is written, input () is added at the end. Why is the window still flashing when The py file is running?
Start, Run, Enter cmd, open the dos simulation window, and enter the python py file path.
Python 26: how to run the saved py file?
We recommend that you use python idle to write your code.
The idle is built in by default when you install python. You can open "START" and search "python" directly. Note that you need to select an idle
After entering, you can directly run the code, print, or something in the idle shell.
Or press "ctrl + n" to write a. py file and run
Happy coding!