# -- coding: utf-8 -- import cProfile import pstats left_column_width = 40 loop_range = range(10000000) class A: def __init__(self): self.value = 0 @property def x(self): value = self.value self.value += 1 return value class B: def __init__(self, y): self.y = y def test1(): for z in loop_range: pass def test2(): b = B(A()) for z in loop_range: if b.y.x: pass def test3(): b = B(A()) a = b.y for z in loop_range: if a.x: pass 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') print('number of loops:'.ljust(left_column_width, ' '), len(loop_range), '\n') print("pass:".ljust(left_column_width, ' '), pstats.Stats(test1_profile).total_tt) print("if b.y.x:".ljust(left_column_width, ' '), pstats.Stats(test2_profile).total_tt) print("if a.x:".ljust(left_column_width, ' '), pstats.Stats(test3_profile).total_tt, '\n')