Efficient searching through objects

Discussion in 'Python' started by sert, Feb 26, 2009.

  1. sert

    sert Guest

    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?
     
    sert, Feb 26, 2009
    #1
    1. Advertising

  2. sert

    Guest

    sert <> wrote:
    > 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
     
    , Feb 26, 2009
    #2
    1. Advertising

  3. sert

    Guest

    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
     
    , Feb 26, 2009
    #3
  4. sert

    sert Guest

    wrote in
    news:
    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.
     
    sert, Feb 26, 2009
    #4
  5. sert

    Tim Rowe Guest

    2009/2/26 sert <>:
    > wrote in
    > news:
    > 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.


    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.

    --
    Tim Rowe
     
    Tim Rowe, Feb 26, 2009
    #5
  6. sert

    Tim Chase Guest

    > 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
     
    Tim Chase, Feb 26, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alf
    Replies:
    1
    Views:
    319
    Gabriel Genellina
    Aug 29, 2006
  2. Henrik Goldman

    Efficient searching through stl map?

    Henrik Goldman, Apr 9, 2006, in forum: C++
    Replies:
    3
    Views:
    721
    Daniel T.
    Apr 9, 2006
  3. 7stud
    Replies:
    11
    Views:
    755
    Dennis Lee Bieber
    Mar 20, 2007
  4. Replies:
    1
    Views:
    392
    Victor Bazarov
    May 10, 2007
  5. stumblng.tumblr
    Replies:
    1
    Views:
    237
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page