Python find efficiency comparison in list and dict

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/wzgbm/article/details/54691615

First give a simple example, the time to test the list and dict find:

ImportTime Query_lst = [-60000,-6000,- -,- -,-6,0,6, -, -,6000,60000] LST = [] dic = {} forIinchRange100000000): Lst.append (i) dic[i] =1Start = Time.time () forVinchQuery_lst:ifVinchLst:ContinueEnd1 = Time.time () forVinchQuery_lst:ifVinchDic:ContinueEnd2 = Time.time ()Print "List Search time:%f"% (End1-start)Print "Dict Search Time:%f"% (END2-END1)
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21st
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21st
  • 22
  • 23
  • 24
  • 25
  • 26

Operation Result:

List Search time:11.836798
Dict Search time:0.000007

We can see from the above example that the lookup efficiency of list is much lower than the efficiency of dict, for the following reasons:

The storage structure of a list object in Python uses a linear table, so its query complexity is O (n), while the storage structure of the Dict object uses a hash table, which in its best case is an O (1) query complexity.

Python find efficiency comparison in list and dict

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.