Surprising results with Python timeit: Counter() vs defaultdict() vs dict()
I obtained very surprising results with timeit, can someone tell me if I am doing something wrong ? I am using Python 2.7. This is the contents of file speedtest_init.py: import random to_count = [random.randint(0, 100) for r in range(60)] These are the contents of speedtest.py: __author__ = 'BlueTrin' import timeit def test_init1(): print(timeit.timeit('import speedtest_init')) def test_counter1(): s = """\ d = defaultdict(int); for i in speedtest_init.to_count: d[i] += 1 """ print(timeit.timeit(s, 'from collections import defaultdict; import speedtest_init;')) def test_counter2(): print