473,414 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

The fastest search

***********************
Your mail has been scanned by InterScan MSS.
***********************
Hello,

I'm poor in knoweledge of python, sorry. What's the fastest result between :

if item in alist:
do_something

or

if adictionay has_key(item):
do_something

Is there some trick to apply the best search in wise use of resources while
using the above said methods?

F

Oct 21 '06 #1
4 1353
On Sat, 21 Oct 2006 17:41:04 +0800, Fulvio wrote:
I'm poor in knoweledge of python, sorry. What's the fastest result between :

if item in alist:
do_something

or

if adictionay has_key(item):
do_something
Let's find out.

Searches that succeed:
>>import timeit
search_list_setup = "L = range(10000)"
search_list = "L.index(7500)"
timeit.Timer(search_list, search_list_setup).timeit(10000)
8.0721840858459473
>>search_dict_setup = """D = {}
.... for i in range(10000):
.... D[i] = None
.... """
>>search_dict = "D.has_key(7500)"
timeit.Timer(search_dict, search_dict_setup).timeit(10000)
0.0079340934753417969

So for searches that succeed, dicts are much faster than lists.

How about searches that fail?

>>search_dict_fail = "D.has_key(-7500)"
timeit.Timer(search_dict_fail, search_dict_setup).timeit(10000)
0.0060589313507080078
>>search_list_fail = """try:
.... L.index(-7500)
.... except ValueError:
.... pass
.... """
>>timeit.Timer(search_list_fail, search_list_setup).timeit(10000)
11.371721982955933

Again, dicts are much faster.

But what if you know the list is sorted, and you can do a binary search?
>>binary_search_setup = """import bisect
.... L = range(10000)
.... """
>>binary_search = "bisect.bisect(L, 7500)"
timeit.Timer(binary_search, binary_search_setup).timeit(10000)
0.04595494270324707

Still slower than a dict, but much, much faster than a linear search.

Is there some trick to apply the best search in wise use of resources
while using the above said methods?
Yes.

Measure, don't guess. Don't even think about optimising your code until
it is working. Use the data structures which are natural to the task, then
measure to see if it is too slow. Never assume something is too slow until
you've measured it. Measure using realistic data -- don't do all your
tests with lists of ten items if actual working data will have ten
thousand items, and vice versa.

And most importantly, think about whether optimisation is a worthwhile use
of your time: do you really care about saving five milliseconds in a
program that takes 30 seconds to run?
--
Steve.

Oct 21 '06 #2
Fulvio schrieb:
>
Is there some trick to apply the best search in wise use of resources while
using the above said methods?
measure it:

http://docs.python.org/lib/module-timeit.html

Regarding your debugger question in the seperate thread I don't know
since I am not using a debugger at all.

Most people coming from other languages are looking for a debugger and
are overlooking that in python a interactive interpreter, a good editor,
print statements and the tracebacks are all a lot of python developers need.

Small snippets of code are developed in the interpreter and when they
are working assembled in the editor. If something goes wrong a print on
the suspect place or the traceback is all I need. (of course this is
matter of taste)

--
Servus, Gregor
Oct 21 '06 #3
***********************
Your mail has been scanned by InterScan MSS.
***********************
On Saturday 21 October 2006 19:09, Steven D'Aprano wrote:
So for searches that succeed, dicts are much faster than lists.
Very precious advice. Thank you, indeed. The lesson was good :-)

I'd only like to know if "List.index(vars)" has the same performance than
"vars in List" or even it's the same thing.
But, so far I still have my own question about that the search goes on the
key's name rather then its paired value. In fact, in my actual case the value
doesn't make any use.
However the search goes on a few hundred email ID, the difference is less than
a jiffy :-)

F
Oct 22 '06 #4
***********************
Your mail has been scanned by InterScan MSS.
***********************
On Saturday 21 October 2006 19:40, Gregor Horvath wrote:
Small snippets of code are developed in the interpreter and when they
are working assembled in the editor. If something goes wrong a print on
the suspect place or the traceback is all I need. (of course this is
matter of taste)
I also using this techinque, but I found myself to do a lot of rewriting on
the code, as long as probing several points on the program.
The pdb.py is much of what I need. Running the program can stop in critical
points and see if all comes as expected. It won't need to remove
several "print" from the code.
Obvious, it's referred for programs which aren't time critical.

F

Oct 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Simon | last post by:
Hi, If I have a string, (variable len), and I am looking for the first position of one char in array starting from position 'x' For example, // the 'haystack' $string = "PHP is great,...
2
by: tommazzo | last post by:
Hi! I'm looking for a way to find the position of a certain pattern within a string. On my search on the internet I've come accross various algorithms such as Knuth-Morris-Pratt and Boyer-Moore...
6
by: Jonathan | last post by:
I am hoping that someone more experienced than myself can point me towards what might be the fastest data lookup method to use for storing ip addresses. My situation is that I will need to maintain...
9
by: danny van elsen | last post by:
hello all, I have an application in which I build a list<node>, with potentially thousands of nodes. each node has an "index", and all nodes are ordered by this index. this index reflects a...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
2
by: UJ | last post by:
I have a dataset that will have say 10000 records in it with the names of files that are used by the system. I then have a large directory of files that correspond to that list of files and want to...
3
by: Harry Haller | last post by:
What is the fastest way to search a client-side database? I have about 60-65 kb of data downloaded to the client which is present in 3 dynamically created list boxes. The boxes are filled from 3...
1
by: Harry Haller | last post by:
What is the fastest way to search a client-side database? I have about 60-65 kb of data downloaded to the client which is present in 3 dynamically created list boxes. The boxes are filled from 3...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.