Python Dictionary (dict) traversal of four methods Performance Test report

Source: Internet
Author: User
This article mainly tests the performance of Python dictionary dict traversal methods to find out the most efficient method. In python, there are four ways to traverse dict. But what is the performance of the four traversal methods? I did the following test

L = [(x, x) for x in xrange (10000)] d = dict (l) from time import clockt0 = clock () for I in d: t = I + d [I] t1 = clock () for k, v in d. items (): t = k + vt2 = clock () for k, v in d. iteritems (): t = k + vt3 = clock () for k, v in zip (d. iterkeys (), d. itervalues (): t = k + vt4 = clock () print t1-t0, t2-t1, t3-t2, t4-t3

Run the script five times and the result is as follows:

Python test. py0.00184039735833 0.00326492977712 0.00214993552657 0.00311549755797 python test. py0.00182356570728 0.00339342506446 0.00234863111466 0.00321566640817 python test. py0.00185104108827 0.00324563495762 0.00211175641563 0.00313479237748 python test. py0.0018215130669 0.00320950848705 0.00215814608806 0.00322798225041 python test. Release 0.00391807994377 0.00207604047314 0.00322757172233

Obviously, the first method is the most efficient, while the third method is slightly less efficient, and the performance of the second method is much worse.
However, the actual difference is not too big, so you don't have to worry too much about it.

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.