# -- coding: utf-8 -- import cProfile import pstats left_column_width = 35 loop_range = range(1000000) class Class1: def __init__(self): self.a = 1 self.b = 0 self.c = None self.z = 2 class Class2: def __init__(self): self.a = 1 self.call1() self.c = None self.z = 2 def call1(self): self.b = 0 class Class3: def __init__(self): self.a = 1 self.call1() self.c = None self.call2() def call1(self): self.b = 0 def call2(self): self.z = 2 class Class4: def __init__(self): self.call0() self.call1() self.c = None self.call2() def call0(self): self.a = 1 def call1(self): self.b = 0 def call2(self): self.z = 2 class Class5: def __init__(self): self.call0() self.call1() self.call3() self.call2() def call0(self): self.a = 1 def call1(self): self.b = 0 def call2(self): self.z = 2 def call3(self): self.c = None def test1(): for x in loop_range: a = Class1() def test2(): for x in loop_range: a = Class2() def test3(): for x in loop_range: a = Class3() def test4(): for x in loop_range: a = Class4() def test5(): for x in loop_range: a = Class5() 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') print('number of loops:'.ljust(left_column_width, ' '), len(loop_range), '\n') print('__init__ with 0 function calls:'.ljust(left_column_width, ' '), pstats.Stats(test1_profile).total_tt, '\n') print('__init__ with 1 function call:'.ljust(left_column_width, ' '), pstats.Stats(test2_profile).total_tt, '\n') print('__init__ with 2 function calls:'.ljust(left_column_width, ' '), pstats.Stats(test3_profile).total_tt, '\n') print('__init__ with 3 function calls:'.ljust(left_column_width, ' '), pstats.Stats(test4_profile).total_tt, '\n') print('__init__ with 4 function calls:'.ljust(left_column_width, ' '), pstats.Stats(test5_profile).total_tt, '\n')