Comparison of Two Methods of traversing Dictionary (dict) in python

Source: Internet
Author: User

With its elegant syntax and convenient built-in data structure, python has won the favor of many programmers.
There is a very useful data structure, that is, the dictionary (dict), which is very simple to use. Speaking of traversing a dict structure, I think most people will think of the for key in dictobj method. It is true that this method is applicable in most cases. But it is not completely secure. Please refer to the following example:
Copy codeThe Code is as follows:
# Initialize a dict
>>> D = {'A': 1, 'B': 0, 'C': 1, 'D': 0}
# The intention is to traverse dict. If the element value is 0, delete it.
>>> For k in d:
... If d [k] = 0:
... Del (d [k])
...
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration
# An exception is thrown in the result. Only one element of 0 is deleted.
>>> D
{'A': 1, 'C': 1, 'D': 0}

>>> D = {'A': 1, 'B': 0, 'C': 1, 'D': 0}
# D. keys () is an array of the lower mark
>>> D. keys ()
['A', 'C', 'B', 'D']
# In this way, the traversal is okay, because actually, the list constant d. keys () is traversed here.
>>> For k in d. keys ():
... If d [k] = 0:
... Del (d [k])
...
>>> D
{'A': 1, 'C': 1}
# The result is correct.
>>>
In fact, this example is simplified. I found this problem in a multi-threaded program. So, my suggestion is: when traversing dict, use for k in d. keys.
But is it absolutely safe if it is multi-threaded? Not necessarily: When both threads have completed d. after keys (), if both threads Delete the same key, the deletion will succeed, and the deleted thread will certainly report a KeyError, which seems to be guaranteed by other methods.


Another article: Performance Comparison between the two dict traversal methods

Performance issues related to parentheses and no parentheses in dict Traversal
Copy codeThe Code is as follows:
For (d, x) in dict. items ():
Print "key:" + d + ", value:" + str (x)

For d, x in dict. items ():
Print "key:" + d + ", value:" + str (x)

Performance test results with and without parentheses:

Copy codeThe Code is as follows:
Test Results
Number of tests: 15
Start time with parentheses: 12:13:37. 375000
End Time with parentheses: 12:13:37. 375000
Time Interval: 0: 00: 00
Start time without parentheses: 12:13:37. 375000
End Time without parentheses: 12:13:37. 375000
Time Interval: 0: 00: 00

Number of tests: 50
Start time with parentheses: 12:13:57. 921000
End Time with parentheses: 12:13:57. 921000
Time Interval: 0: 00: 00
Start time without parentheses: 12:13:57. 921000
End Time without parentheses: 12:13:57. 937000
Time Interval: 0: 00: 00.016000
Number of tests: 100
Start time with parentheses: 11:53:57. 453000
End Time with parentheses: 11:53:57. 468000
Time Interval: 0: 00: 00.015000
Start time without parentheses: 11:53:57. 468000
End Time without parentheses: 11:53:57. 531000
Time Interval: 0: 00: 00.063000

Number of tests: 150
Start time with parentheses: 12:00:54. 812000
End Time with parentheses: 12:00:54. 828000
Time Interval: 0: 00: 00.016000
Start time without parentheses: 12:00:54. 828000
End Time without parentheses: 12:00:54. 921000
Time Interval: 0: 00: 00.093000

Number of tests: 200
Start time with parentheses: 11:59:54. 609000
End Time with parentheses: 11:59:54. 687000
Time Interval: 0: 00: 00.078000
Start time without parentheses: 11:59:54. 687000
End Time without parentheses: 11:59:54. 734000
Time Interval: 0: 00: 00.047000

Number of tests: 500
Start time with parentheses: 11:54:39. 906000
End Time with parentheses: 11:54:40. 078000
Time Interval: 0: 00: 00.172000
Start time without parentheses: 11:54:40. 078000
End Time without parentheses: 11:54:40. 125000
Time Interval: 0: 00: 00.047000

Number of tests: 1000
Start time with parentheses: 11:54:49. 171000
End Time with parentheses: 11:54:49. 437000
Time Interval: 0: 00: 00.266000
Start time without parentheses: 11:54:49. 437000
End Time without parentheses: 11:54:49. 609000
Time Interval: 0: 00: 00.172000

Number of tests: 2000
Start time with parentheses: 11:54:58. 921000
End Time with parentheses: 11:54:59. 328000
Time Interval: 0: 00: 00.407000
Start time without parentheses: 11:54:59. 328000
End Time without parentheses: 11:54:59. 687000
Time Interval: 0: 00: 00.359000

Number of tests: 5000
Start time with parentheses: 11:55:05. 781000
End Time with parentheses: 11:55:06. 734000
Time Interval: 0: 00: 00.953000
Start time without parentheses: 11:55:06. 734000
End Time without parentheses: 11:55:07. 609000
Time Interval: 0: 00: 00.875000

Number of tests: 10000
Start time with parentheses: 11:55:15. 656000
End Time with parentheses: 11:55:17. 390000
Time Interval: 0: 00: 01.734000
Start time without parentheses: 11:55:17. 390000
End Time without parentheses: 11:55:19. 109000
Time Interval: 0: 00: 01.719000

Number of tests: 20000
Start time with parentheses: 12:19:14. 921000
End Time with parentheses: 12:19:18. 593000
Time Interval: 0: 00: 03.672000
Start time without parentheses: 12:19:18. 593000
End Time without parentheses: 12:19:22. 218000
Time Interval: 0: 00: 03.625000


We can see that when the number of dict entries is 200, the performance with parentheses is relatively higher, but the execution time without parentheses is shorter after more than 200 pieces of data.

The following is the test code:

Copy codeThe Code is as follows:
Test Code
#-*-Coding: UTF-8 -*-
Import datetime, codecs

Dict = {}

For I in xrange (0, 20000 ):
Dict. setdefault ("name" + str (I ))
Dict ["name" + str (I)] = "name"

S = codecs. open (r 'C: \ dict.txt ', 'A', 'utf-8 ')

Def write (des ):
S. write (des. decode ("UTF-8 "))

Write ("Number of test results :")
Write (str (len (dict) + "\ r \ n ")
Write ("Start Time with parentheses :")
A = datetime. datetime. now ()
S. write (str (a) + "\ r \ n ")

For (d, x) in dict. items ():
Print "key:" + d + ", value:" + str (x)
Write ("End Time with parentheses :")
B = datetime. datetime. now ()
Write (str (B) + "\ r \ n ")
Write ("interval :")
Write (str (B-a) + "\ r \ n ")

Write ("Start time without parentheses :")
C = datetime. datetime. now ()
Write (str (c) + "\ r \ n ")
For d, x in dict. items ():
Print "key:" + d + ", value:" + str (x)
Write ("End Time without parentheses :")
D = datetime. datetime. now ()
Write (str (d) + "\ r \ n ")
Write ("interval :")
Write (str (d-c) + "\ r \ n ")
Write ("\ r \ n ")
S. close ()

Is there a good solution to Chinese Garbled text ....?

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.