john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

set immutableset list performance object compare override equals hash

alphabet = set()
alphabet.add('a')
alphabet.update(['b', 'c', 'd'])

letters = ['a', 'b', 'c']
print alphabet.issuperset(letters)  # True, the operator understands the parameter even if a list

# http://en.wikibooks.org/wiki/Python_Programming/Sets

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 'usable as a dict key' is the main reason to use ImmutableSet (now frozenset)

import time
biglist = list( range( 500000 ) )
bigset = set( range( 500000 ) )    # 500,000 integers
bigimmutableset = frozenset( range( 500000 ) )  # 500,000 integers

assert len( bigset ) == len( biglist )
assert len( bigset ) == len( bigimmutableset )


def tester( iterable , test_range ):
    t = time.time()
    for i in range( 100 ):      # 100 repetitions
        for item in test_range:          # 5000 repetitions of 100 to 5000
            item in iterable
    return time.time() - t # time returned is for 100 loops

print ''
print '%f biglist %s range( 100,5000)' % ( tester(  biglist , range( 100 , 500 )  ) , str( type( biglist ) )  )
print '%f bigset %s range( 100,5000)' % ( tester(  bigset , range( 100 , 500 )  ) , str( type( bigset ) )  )
print '%f bigimmutableset %s range( 100,5000)' % ( tester(  bigimmutableset , range( 100 , 500 )  ) , str( type( bigimmutableset ) )  )


start = time.time()
my_list = list( bigset )
print '%f list( bigset ) ' % ( time.time() - start )

start = time.time()
my_list = list( bigimmutableset )
print '%f list( bigimmutableset ) ' % ( time.time() - start )

start = time.time()
my_set = set( biglist )
print '%f set( biglist ) ' % ( time.time() - start )

start = time.time()
my_set = sets.Set( biglist )
print '%f sets.Set( biglist ) ' % ( time.time() - start )

start = time.time()
my_set = sets.ImmutableSet( biglist )
print '%f immutableset( biglist ) ' % ( time.time() - start )

"""
0.299000 biglist <type 'list'> range( 100,5000)
0.024000 bigset <class 'sets.Set'> range( 100,5000)
0.020000 bigimmutableset <class 'sets.ImmutableSet'> range( 100,5000)
0.010000 list( bigset )
0.015000 list( bigimmutableset )
0.037000 set( biglist )
0.074000 sets.Set( biglist )
0.075000 immutableset( biglist )
"""

# my_set = set( my_list )       # TypeError: hashable

# no_duplicate_list = list( my_set )
# x in my_set    # True or False

# t,u,v can be any iterable
# x in t, x not in t
# my_set.isdisjoint( t )        # no overlap
# my_set.issubset( t )
# my_set.issuperset( t )
# larger_set = my_set.union( t , u , v )    # set | other |
# common_set = my_set.intersection( t , u  , v )    # set & other &
# inverted_intersection_set = my_set.symmetric_difference( t )  # set ^ other
# subtract_t_u_v_sets = my_set.difference( t , u , v )     # set - other


# my_set.update( t , u , v )    #  similar to union but modifies the current set
# my_set.intersection_update( t , u  , v )     # similar to intersection but modifies the current set
# my_set.difference_update( t , u , v )     # similar to difference but modifies the current set
# my_set.symmetric_difference_update( t , u , v )   # similar to symmetric_difference but modifies the current set

# my_set.add( element )
# my_set.pop( element )         # returns the element and returns a KeyError if not contained in the set
# my_set.remove( element )      # removes and KeyError if not contained in the set
# my_set.discard( element )     # removes if present
# my_set.clear()                # removes all from the set

# using sets over objects:
# class MyObject:
# def __eq__(self, other):      # sometimes only need to override this
#    return self.author_name==other.author_name and self.title==other.title
# def __hash__(self)  # http://docs.python.org/2/glossary.html#term-hashable
# return hash(('title', self.title, 'author_name', self.author_name))

  • « pytest command line param skip test
  • html lists »

Published

Feb 4, 2014

Category

python

~405 words

Tags

  • compare 4
  • equals 4
  • hash 7
  • immutableset 1
  • list 23
  • object 7
  • override 4
  • performance 6
  • python 180
  • set 5