| | |
Which is faster, a Dictionary Lookup or an Index Operation?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
If you want to check if the username is present, the easiest thing to do is:
python Syntax (Toggle Plain Text)
usern=['sravan953','Dan08','SuperMetroid','vegaseat'] 'SuperMetroid' in usern """ >> True """
I don't know exactly what you want to compare, but here is a code which measures the time necessary to execute 1,000,000 times a dictionary lookup (the statement
For your problem, I would choose a dictionary lookup over other methods.
'7498' in D ) python Syntax (Toggle Plain Text)
from timeit import Timer D = dict() for i in range(10000): D[str(i)] = i print(Timer("'7498' in D", "from __main__ import D").timeit())
Dictionary key searches are highly optimized, since Python itself uses dictionaries internally. There are entire articles published that recommend converting a long list into a dictionary for fast searches. Still faster than a list search even with the time it takes to convert.
I remember seeing one of these articles in:
http://code.activestate.com/recipes/langs/python/
I remember seeing one of these articles in:
http://code.activestate.com/recipes/langs/python/
Last edited by vegaseat; Oct 4th, 2009 at 4:15 pm. Reason: activestate
May 'the Google' be with you!
I got curious, so I did a little test run ...
python Syntax (Toggle Plain Text)
# use Python3's dictionary comprehension to # speed up list searches # # the time it takes to create the dictionary # will be regained after about 6 searches # as list size increases this goes down to > 1 search # tested with Python 3.1.1 vegaseat import timeit data = """\ Bill Brutus Daphne Dorky Al Kristin Cloe Carlos Pete Pheobe Jerry Jack Justin John Julie Joe Moe Theo Albert Alberto Pauline Paula Christopher Gisela Lamar Donna Demitrius Frank Heidi Margot Cindy Doris Harry Larry Dilbert Mary Robert Sophia Samuel Candy Tawny Terry Markus Veronika Norbert Zoe Udo""" # create a list of names from the data mylist = data.split('\n') # create a dictionary with name:zero pairs from the list mylist_d = {name:0 for name in mylist} # search for 'Udo' is the last item in the list and dictionary statement = "'Udo' in mylist" setup = "from __main__ import mylist" t = timeit.Timer(statement, setup) # doing 1000000 passes (default) gives microseconds per pass elapsed = t.timeit() sf = "Code %-20s takes %0.3f micro-seconds/pass" print( sf % (statement, elapsed)) statement = "'Udo' in mylist_d" setup = "from __main__ import mylist_d" t = timeit.Timer(statement, setup) elapsed = t.timeit() sf = "Code %-20s takes %0.3f micro-seconds/pass" print( sf % (statement, elapsed)) print('-'*60) statement = "{name:0 for name in mylist}" setup = "from __main__ import mylist" t = timeit.Timer(statement, setup) elapsed = t.timeit() sf = "Code %-20s takes %0.3f micro-seconds/pass" print( sf % (statement, elapsed)) print('-'*60) # optional tests to show the last 4 items in list and dictionary print("Last 4 items in list and dictionary:") print(mylist[-4:]) print(list(mylist_d.keys())[-4:]) """my result --> Code 'Udo' in mylist takes 2.193 micro-seconds/pass Code 'Udo' in mylist_d takes 0.182 micro-seconds/pass ------------------------------------------------------------ Code {name:0 for name in mylist} takes 11.151 micro-seconds/pass ------------------------------------------------------------ Last 4 items in list and dictionary: ['Veronika', 'Norbert', 'Zoe', 'Udo'] ['Dilbert', 'Julie', 'Al', 'Udo'] """
May 'the Google' be with you!
![]() |
Similar Threads
- JSP database connectivity according to Model View Controller (MVC) Model 2 (JSP)
- help -- my for loop only does one line? (Python)
- Turn Off Indexing to Speed Up XP (Windows tips 'n' tweaks)
- Array - Very Urgent (C++)
- Pulling Data from two tables (PHP)
- string search help (C++)
- Template Help (C++)
- [basics] LZ(W) compression explanation. (C)
Other Threads in the Python Forum
- Previous Thread: Python program, need help please.
- Next Thread: Help with a section of code
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied advanced application argv beginner change code color command csv def dictionary dynamic edit editing enter event examples excel file float format function google gui homework import inches input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric obexftp output parameters parsing path port prime programming projects py2exe pygame pygtk pyopengl pyqt python random recursion remote return reverse scrolledtext session simple skinning smtp software sprite statictext stderr string strings strip syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable voip windows wxpython






