New version of Python-Python 3.0

Source: Internet
Author: User

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.

 
 
  1.  from __future__ import with_statement  
  2.  
  3. import sys  
  4.  
  5. import time  
  6.  
  7. if sys.version_info[0] == 3:  
  8.  
  9. exec("c = b'X'")  
  10.  
  11. else:  
  12.  
  13. c = 'X' 
  14.  
  15. def test_write_speed():  
  16.  
  17. start = time.time()  
  18.  
  19. with open('1.txt', 'wb') as f:  
  20.  
  21. for i in range(5000000):  
  22.  
  23. f.write(c)  
  24.  
  25. end = time.time() - start  
  26.  
  27. print (end)  
  28.  
  29. return end  
  30.  
  31. times = [test_write_speed() for i in range(10)]  
  32.  
  33. times.remove(max(times))  
  34.  
  35. times.remove(min(times))  
  36.  
  37. 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:

 
 
  1. * Python 2.5 - 3.0146874487400055  
  2.  
  3. * Python 2.6 - 4.4676837027072906  
  4.  
  5. * Python 3.0 - 33.0755852461  
  6.  
  7. * 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:

 
 
  1. * Python 2.5 - 3.1337025165557861  
  2.  
  3. * Python 2.6 - 2.9250392615795135  
  4.  
  5. * Python 3.0 - 68.4243619442  
  6.  
  7. * 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.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.