class Node:
    def __init__(self, data):
        self.data = data

    def __repr__(self):
        return "Node(%s)"%`self.data`

class IndexGenerator:
    def __init__(self):
        self.index = -1

    def next(self):
        self.index += 1
        return self.index
        
class HandledNode:
    def __init__(self, data, HandleGenerator=IndexGenerator()):
        self.data = data
        self.handle = HandleGenerator.next()

    def __hash__(self):
        return self.handle

    def __repr__(self):
        return "Node(%s)"%`self.data`

def test_lookup_speed():
    import time, random
    numtests = 10000
    numlookups = 1000
    nodes = []
    handledNodes = []
    dict = {}
    handledDict = {}
    for i in range(numtests):
        node = Node(i)
        handledNode = HandledNode(i)
        dict[node] = i
        handledDict[handledNode.handle] = i
        nodes.append(node)
        handledNodes.append(handledNode)

    # check the lookups for objects
    t1 = time.time()
    for i in range(numlookups):
        index = random.randrange(numtests)
        node = nodes[index]
        lookupNode = dict[node]
        assert lookupNode == index
    t2 = time.time()
    print "time for lookups of instances", t2-t1
    
    # check the lookup for 'handles'
    normalTime = t2-t1
    t1 = time.time()
    for i in range(numlookups):
        index = random.randrange(numtests)
        node = handledNodes[index]
        lookupNode = handledDict[node.handle]
        assert lookupNode == index
    t2 = time.time()
    print "time for lookups of integers", t2-t1 

if __name__ == "__main__":
    test_lookup_speed()
    
    
        
