from random import shuffle
from timeit import timeit
from collections import defaultdict, Counter

def dict1():
    d = {}
    for num in nums:
        if num in d:
            d[num] += 1
        else:
            d[num] = 1

def dict2():
    d = {}
    for num in nums:
        d[num] = d.get(num, 0) + 1

def dict3():
    class MyDict(dict):
        def __missing__(self, key):
            return 0
    d = MyDict()
    for num in nums:
        d[num] += 1

def defaultdict1():
    d = defaultdict(int)
    for num in nums:
        d[num] += 1

def Counter1():
    d = Counter()
    for num in nums:
        d[num] += 1

def Counter2():
    d = Counter(nums)

nums = range(1000) * 1000
shuffle(nums)
for _ in range(2):
    for func in dict1, dict2, dict3, defaultdict1, Counter1, Counter2:
        print '%.3f' % timeit(func, number=2), func.__name__
    print