def benchmark(test):
    def measure(test):
        import time
        start = time.clock()
        test()
        return time.clock() - start
    import gc
    if gc.isenabled():
        time_with_gc = measure(test)
        gc.disable()
        gc.collect()
        time_without_gc = measure(test)
        gc.enable()
    else:
        gc.collect()
        time_without_gc = measure(test)
        gc.enable()
        time_with_gc = measure(test)
        gc.disable()
    print ('%7.2f gc-on %7.2f gc-off %7.2f ratio    %s'
           % (time_with_gc, time_without_gc, time_with_gc / time_without_gc,
              test.__name__))

list1 = range(0, 10000) + range(5000, 15000) + range(90000, 100000)
list2 = range(0, 2000) + range(40000, 50000) + range(95000, 105000)

def test_comprehension():
    dict([(k,1) for k in list1+list2]).keys()

def test_zip_and_update():
    result = dict(zip(list1, list1))
    result.update(dict(zip(list2, list2)))
    result.keys()

def test_zip_with_none():
    dict(zip(list1 + list2, [None] * (len(list1) + len(list2)))).keys()

def test_zip_with_self():
    both = list1 + list2
    dict(zip(both, both)).keys()

benchmark(test_comprehension)
benchmark(test_zip_and_update)
benchmark(test_zip_with_none)
benchmark(test_zip_with_self)
