Dicts 5x Faster than Sets

Discussion in 'Python' started by Kamilche, Jun 9, 2004.

  1. Kamilche

    Kamilche Guest

    Hm, I just saw the 'sets' feature, and ran some timings to see if I
    should use it instead of 'dict' sometimes. I discovered it's 4x slower
    in adding, and 6x slower in removing items!

    Here's the code, in case you're interested.

    from sets import Set
    import mytime

    '''
    Set Slower than Dict

    Sample results:

    Function Loops Seconds Loops/sec
    ***********************************************
    Set add 1000000 1.391 718907
    Set delete 1000000 1.14 877193
    Dict add 1000000 0.344 2906975
    Dict delete 1000000 0.172 5813955

    '''

    def test():
    tmr = mytime.Timer()
    max = 1000000
    s = range(max)
    print tmr.heading

    s1 = Set()
    tmr.startit()
    for i in s:
    s1.add(i)
    tmr.stopit(max)
    print tmr.results('Set add')
    tmr.startit()
    for i in s:
    s1.remove(i)
    tmr.stopit(max)
    print tmr.results('Set delete')


    tmr.startit()
    s2 = {}
    for i in s:
    s2 = i
    tmr.stopit(max)
    print tmr.results('Dict add')
    tmr.startit()
    for i in s:
    del s2
    tmr.stopit(max)
    print tmr.results('Dict delete')

    test()
     
    Kamilche, Jun 9, 2004
    #1
    1. Advertising

  2. Kamilche

    David Wilson Guest

    On Wed, Jun 09, 2004 at 12:24:25PM -0700, Kamilche wrote:

    > Hm, I just saw the 'sets' feature, and ran some timings to see if I
    > should use it instead of 'dict' sometimes. I discovered it's 4x slower
    > in adding, and 6x slower in removing items!


    Which version of Python did you test this on? In the upcoming Python
    2.4, sets will be significantly faster, so don't discard their use yet,
    as they'll be implemented using native code within the year.


    David.

    --
    "Science is what we understand well enough to explain to a
    computer. Art is everything else we do."
    -- Donald Knuth
     
    David Wilson, Jun 9, 2004
    #2
    1. Advertising

  3. Kamilche

    Kamilche Guest

    David Wilson <> wrote in message news:<>...

    > Which version of Python did you test this on?


    2.3.4
     
    Kamilche, Jun 10, 2004
    #3
  4. Kamilche

    Aahz Guest

    In article <>,
    Kamilche <> wrote:
    >
    >Hm, I just saw the 'sets' feature, and ran some timings to see if I
    >should use it instead of 'dict' sometimes. I discovered it's 4x slower
    >in adding, and 6x slower in removing items!


    Speed is not the raison d'etre for sets, convenience and clarity are.
    You are better off using sets if you need set intersection and union,
    for example.

    Keep in mind that dicts are probably the single most highly optimized
    chunk of code in Python, and that sets are built on top of dicts.
    Unless you need the absolute utmost in speed, the penalty you're paying
    for the wrapper code shouldn't be an issue.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "as long as we like the same operating system, things are cool." --piranha
     
    Aahz, Jun 10, 2004
    #4
    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. Alan Isaac

    docs patch: dicts and sets

    Alan Isaac, May 12, 2007, in forum: Python
    Replies:
    5
    Views:
    259
  2. CMOS
    Replies:
    15
    Views:
    478
    James Kanze
    May 17, 2007
  3. Alan Isaac

    docs patch: dicts and sets

    Alan Isaac, May 15, 2007, in forum: Python
    Replies:
    12
    Views:
    491
  4. John Nagle

    Performance: sets vs dicts.

    John Nagle, Aug 29, 2010, in forum: Python
    Replies:
    37
    Views:
    2,622
    Robert Kern
    Sep 3, 2010
  5. bruce
    Replies:
    0
    Views:
    250
    bruce
    Jan 10, 2012
Loading...

Share This Page