Efficient searching through objects

S

sert

I have written a program that reads data and updates the records
for some people. They are represented by objects, and I need to
read the data from a file, look the person up and then update
his record.

I have implemented this by creating a list with all the people's
names and another list with their objects (their data).

It works but after profiling the code it turns out that half the
time spent in the program is spent in the list.index() function
looking up names. Isn't there a less goofy and more efficient
way of doing this?
 
R

rdmurray

sert said:
I have written a program that reads data and updates the records
for some people. They are represented by objects, and I need to
read the data from a file, look the person up and then update
his record.

I have implemented this by creating a list with all the people's
names and another list with their objects (their data).

It works but after profiling the code it turns out that half the
time spent in the program is spent in the list.index() function
looking up names. Isn't there a less goofy and more efficient
way of doing this?

It sounds like what you are looking for is the dictionary
data type.

--RDM
 
B

bearophileHUGS

sert:
I have implemented this by creating a list with all the people's
names and another list with their objects (their data).

It works but after profiling the code it turns out that half the
time spent in the program is spent in the list.index() function
looking up names. Isn't there a less goofy and more efficient
way of doing this?

Try using a dict instead, where keys are the names and objects the
values (it turns a linear search in a quick hash look up). . Then tell
us the performance changes.

Bye,
bearophile
 
S

sert

(e-mail address removed) wrote in
ups.com:
Try using a dict instead, where keys are the names and
objects the values (it turns a linear search in a quick
hash look up). . Then tell us the performance changes.

It halved the overall execution time. Thanks.
 
T

Tim Rowe

2009/2/26 sert said:
(e-mail address removed) wrote in
ups.com:


It halved the overall execution time. Thanks.

Good to see that you've already got an improvement. For what it's
worth, this seems to be classic relational database stuff, and a
relational database will be optimised to do these operations and so is
likely to be faster still. I think there are a couple that Python
works well with, but I've never looked into that -- others will no
doubt be along with recommendations now I've raised the subject.
 
T

Tim Chase

relational database will be optimised to do these operations and so is
likely to be faster still. I think there are a couple that Python
works well with, but I've never looked into that -- others will no
doubt be along with recommendations now I've raised the subject.

batteries-included support for sqlite[1] was added in 2.5 which
would work pretty well for this application without the overhead
(resource OR administrative) of something like MySQL or PostgreSQL.

-tkc

[1]
http://docs.python.org/library/sqlite3.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top