# -- coding: utf-8 -- from collections import defaultdict import cProfile import pstats left_column_width = 40 loop_range = range(2000000) def test1(): a = {}.fromkeys(loop_range, 0) def test2(): a = {i: 0 for i in loop_range} def test3(): a = {} for i in loop_range: a[i] = 0 def test4(): a = {} for i in loop_range: if i not in a: a[i] = 0 def test5(): a = dict((i, 0) for i in loop_range) def test6(): a = {} for i in loop_range: a.setdefault(i, 0) def test7(): b = defaultdict(int) for i in loop_range: b[i] def test8(): a = {}.fromkeys(loop_range, 1) def test9(): a = {i: 1 for i in loop_range} def test10(): a = {} for i in loop_range: a[i] = 1 def test11(): a = {} for i in loop_range: if i not in a: a[i] = 1 def test12(): a = dict((i, 1) for i in loop_range) def test13(): a = {} for i in loop_range: a.setdefault(i, 1) def test14(): c = defaultdict(lambda: 1) for i in loop_range: c[i] def test15(): a = {}.fromkeys(loop_range, {}) def test16(): a = {i: {} for i in loop_range} def test17(): a = {} for i in loop_range: a[i] = {} def test18(): a = {} for i in loop_range: if i not in a: a[i] = {} def test19(): a = dict((i, {}) for i in loop_range) def test20(): a = {} for i in loop_range: a.setdefault(i, {}) def test21(): d = defaultdict(dict) for i in loop_range: d[i] test1_profile = cProfile.Profile() test1_profile.runcall(test1) test1_profile.print_stats(sort='cumtime') test2_profile = cProfile.Profile() test2_profile.runcall(test2) test2_profile.print_stats(sort='cumtime') test3_profile = cProfile.Profile() test3_profile.runcall(test3) test3_profile.print_stats(sort='cumtime') test4_profile = cProfile.Profile() test4_profile.runcall(test4) test4_profile.print_stats(sort='cumtime') test5_profile = cProfile.Profile() test5_profile.runcall(test5) test5_profile.print_stats(sort='cumtime') test6_profile = cProfile.Profile() test6_profile.runcall(test6) test6_profile.print_stats(sort='cumtime') test7_profile = cProfile.Profile() test7_profile.runcall(test7) test7_profile.print_stats(sort='cumtime') test8_profile = cProfile.Profile() test8_profile.runcall(test8) test8_profile.print_stats(sort='cumtime') test9_profile = cProfile.Profile() test9_profile.runcall(test9) test9_profile.print_stats(sort='cumtime') test10_profile = cProfile.Profile() test10_profile.runcall(test10) test10_profile.print_stats(sort='cumtime') test11_profile = cProfile.Profile() test11_profile.runcall(test11) test11_profile.print_stats(sort='cumtime') test12_profile = cProfile.Profile() test12_profile.runcall(test12) test12_profile.print_stats(sort='cumtime') test13_profile = cProfile.Profile() test13_profile.runcall(test13) test13_profile.print_stats(sort='cumtime') test14_profile = cProfile.Profile() test14_profile.runcall(test14) test14_profile.print_stats(sort='cumtime') test15_profile = cProfile.Profile() test15_profile.runcall(test15) test15_profile.print_stats(sort='cumtime') test16_profile = cProfile.Profile() test16_profile.runcall(test16) test16_profile.print_stats(sort='cumtime') test17_profile = cProfile.Profile() test17_profile.runcall(test17) test17_profile.print_stats(sort='cumtime') test18_profile = cProfile.Profile() test18_profile.runcall(test18) test18_profile.print_stats(sort='cumtime') test19_profile = cProfile.Profile() test19_profile.runcall(test19) test19_profile.print_stats(sort='cumtime') test20_profile = cProfile.Profile() test20_profile.runcall(test20) test20_profile.print_stats(sort='cumtime') test21_profile = cProfile.Profile() test21_profile.runcall(test21) test21_profile.print_stats(sort='cumtime') print('number of loops:'.ljust(left_column_width, ' '), len(loop_range), '\n') print("a = {}.fromkeys(loop_range, 0):".ljust(left_column_width, ' '), pstats.Stats(test1_profile).total_tt) print("a = {i: 0 for i in loop_range}:".ljust(left_column_width, ' '), pstats.Stats(test2_profile).total_tt) print("a[i] = 0:".ljust(left_column_width, ' '), pstats.Stats(test3_profile).total_tt) print("if i not in a: | a[i] = 0:".ljust(left_column_width, ' '), pstats.Stats(test4_profile).total_tt) print("a = dict((i, 0) for i in loop_range):".ljust(left_column_width, ' '), pstats.Stats(test5_profile).total_tt) print("a.setdefault(i, 0):".ljust(left_column_width, ' '), pstats.Stats(test6_profile).total_tt) print("b[i]:".ljust(left_column_width, ' '), pstats.Stats(test7_profile).total_tt, '\n') print("a = {}.fromkeys(loop_range, 1):".ljust(left_column_width, ' '), pstats.Stats(test8_profile).total_tt) print("a = {i: 1 for i in loop_range}:".ljust(left_column_width, ' '), pstats.Stats(test9_profile).total_tt) print("a[i] = 1:".ljust(left_column_width, ' '), pstats.Stats(test10_profile).total_tt) print("if i not in a: | a[i] = 1:".ljust(left_column_width, ' '), pstats.Stats(test11_profile).total_tt) print("a = dict((i, 1) for i in loop_range):".ljust(left_column_width, ' '), pstats.Stats(test12_profile).total_tt) print("a.setdefault(i, 1):".ljust(left_column_width, ' '), pstats.Stats(test13_profile).total_tt) print("c[i]:".ljust(left_column_width, ' '), pstats.Stats(test14_profile).total_tt, '\n') print("a = {}.fromkeys(loop_range, {}):".ljust(left_column_width, ' '), pstats.Stats(test15_profile).total_tt) print("a = {i: {} for i in loop_range}:".ljust(left_column_width, ' '), pstats.Stats(test16_profile).total_tt) print("a[i] = {}:".ljust(left_column_width, ' '), pstats.Stats(test17_profile).total_tt) print("if i not in a: | a[i] = {}:".ljust(left_column_width, ' '), pstats.Stats(test18_profile).total_tt) print("a = dict((i, {}) for i in loop_range):".ljust(left_column_width, ' '), pstats.Stats(test19_profile).total_tt) print("a.setdefault(i, {}):".ljust(left_column_width, ' '), pstats.Stats(test20_profile).total_tt) print("d[i]:".ljust(left_column_width, ' '), pstats.Stats(test21_profile).total_tt)