Skip to content

Instantly share code, notes, and snippets.

@paveltyavin
Created May 29, 2016 20:41
Show Gist options
  • Save paveltyavin/ab209b7b5477807441f2a1af84e7e045 to your computer and use it in GitHub Desktop.
Save paveltyavin/ab209b7b5477807441f2a1af84e7e045 to your computer and use it in GitHub Desktop.
python hash tables chaining experiment
import time
import math
N = 1000 # any number greater than 1000
class B(object):
"""
Hash always returns the same value.
Then any hash table based on instances of this class will always contain one chain.
"""
def __hash__(self):
return 0
class C(object):
"""
This class differs a bit from B with a __eq__ method.
Then any hash table will always contain one chain with only one element.
"""
def __hash__(self):
return 0
def __eq__(self, other):
return True
def f(N, klass):
"""counts the time of making a set of instances of the same class"""
t1 = time.clock()
{klass() for _ in range(N)}
return time.clock() - t1
def g(klass):
"""
returns the complexity power of f.
e.g if f ~ O(n^3) then this function will return 3.
"""
a = math.log(f(2 * N, klass), 2)
b = math.log(f(N, klass), 2)
return int(round(a - b))
if __name__ == '__main__':
print(g(B))
print(g(C))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment