The next section focuses on the new version of Python-Python 3.0, which includes the new interface of Python 3.0 and some functional improvements, which have almost doubled the performance of the previous Python language, this is a big pleasure for some technical staff.
However, it not only brings many interesting features to developers, but also improves performance. In the previous article, we introduced the standard library changes in Python 3.1 in detail. This article will introduce the performance improvement of the new version.
A large part of the Python 3.1 Development Plan is about performance. What performance improvements have been made after the release of this version? Next we will introduce them one by one. Python 3.0 was once proud to use Python to implement a new I/O library, but its performance can be imagined-as you may estimate, it is very low.
In Python 3.1, people re-implemented this library using C language, so its performance has been greatly improved, which is about 2 to 20 times faster. To this end, we wrote a piece of code: Write 5,000,000 bytes of content to a file. Write 10 times in a row and calculate the average time consumption. Run the code in Python 2.5, 2.6, 3.0, and 3.1 respectively, and then compare the results.
- from __future__ import with_statement
-
- import sys
-
- import time
-
- if sys.version_info[0] == 3:
-
- exec("c = b'X'")
-
- else:
-
- c = 'X'
-
- def test_write_speed():
-
- start = time.time()
-
- with open('1.txt', 'wb') as f:
-
- for i in range(5000000):
-
- f.write(c)
-
- end = time.time() - start
-
- print (end)
-
- return end
-
- times = [test_write_speed() for i in range(10)]
-
- times.remove(max(times))
-
- times.remove(min(times))
-
- print('Average:', sum(times) / len(times))
Is this result interesting and confusing? For this basic I/O task written to a file byte, is the performance difference between different Python versions obvious? The performance of Python 3.0 has been greatly reduced.
This is understandable, and the reason has been mentioned above. However, the performance of Python 2.6 is 2.5 lower than that of Python 50%, and the performance of Python 3.1 is almost twice that of Python 2.5. For the same test, if the file is opened as a text file (that is, the wb is replaced with w) and the string "1" is written to the file instead of the byte, as follows:
- * Python 2.5 - 3.0146874487400055
-
- * Python 2.6 - 4.4676837027072906
-
- * Python 3.0 - 33.0755852461
-
- * Python 3.1 - 5.7733258903
For the same test, if the file is opened as a text file (that is, the wb is replaced with w) and the string "1" is written to the file instead of the byte, as follows:
- * Python 2.5 - 3.1337025165557861
-
- * Python 2.6 - 2.9250392615795135
-
- * Python 3.0 - 68.4243619442
-
- * Python 3.1 - 3.43869066238
What do we know about it? First, the performance of Python 3.0 is terrible for this task. It takes twice as much time to write characters, and has almost 20 times better performance than Python 3.1. Python 2.5, 2.6, and 3.1 take roughly the same time.
- Introduction to Python system files
- How to correctly use Python Functions
- Detailed introduction and analysis of Python build tools
- Advantages of Python in PythonAndroid
- How to Use the Python module to parse the configuration file?