# -- coding: utf-8 -- import cProfile import pstats class A: def __init__(self, value): self.value = value def __repr__(self): return self.value a = [A('Test'), A('Test2')] b = [A('Test'), A('Test2'), A('Test3'), A('Test4')] c = [A('Test'), A('Test2'), A('Test3'), A('Test4'), A('Test5'), A('Test6')] d = [A('Test'), A('Test2'), A('Test3'), A('Test4'), A('Test5'), A('Test6'), A('Test7'), A('Test8')] e = [A('Test'), A('Test2'), A('Test3'), A('Test4'), A('Test5'), A('Test6'), A('Test7'), A('Test8'), A('Test9'), A('Test10')] left_column_width = 20 loop_range = range(300000) def test1(): for i in loop_range: x = '' for y in a: x += y.__repr__() def test2(): for i in loop_range: x = '' for y in a: x += repr(y) def test3(): for i in loop_range: x = '' for y in b: x += y.__repr__() def test4(): for i in loop_range: x = '' for y in b: x += repr(y) def test5(): for i in loop_range: x = '' for y in c: x += y.__repr__() def test6(): for i in loop_range: x = '' for y in c: x += repr(y) def test7(): for i in loop_range: x = '' for y in d: x += y.__repr__() def test8(): for i in loop_range: x = '' for y in d: x += repr(y) def test9(): for i in loop_range: x = '' for y in e: x += y.__repr__() def test10(): for i in loop_range: x = '' for y in e: x += repr(y) 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') print('number of loops:'.ljust(left_column_width, ' '), len(loop_range), '\n') print('__repr__ a:'.ljust(left_column_width, ' '), pstats.Stats(test1_profile).total_tt) print('repr a:'.ljust(left_column_width, ' '), pstats.Stats(test2_profile).total_tt, '\n') print('__repr__ b:'.ljust(left_column_width, ' '), pstats.Stats(test3_profile).total_tt) print('repr b:'.ljust(left_column_width, ' '), pstats.Stats(test4_profile).total_tt, '\n') print('__repr__ c:'.ljust(left_column_width, ' '), pstats.Stats(test5_profile).total_tt) print('repr c:'.ljust(left_column_width, ' '), pstats.Stats(test6_profile).total_tt, '\n') print('__repr__ d:'.ljust(left_column_width, ' '), pstats.Stats(test7_profile).total_tt) print('repr d:'.ljust(left_column_width, ' '), pstats.Stats(test8_profile).total_tt, '\n') print('__repr__ e:'.ljust(left_column_width, ' '), pstats.Stats(test9_profile).total_tt) print('repr e:'.ljust(left_column_width, ' '), pstats.Stats(test10_profile).total_tt, '\n')