# -- coding: utf-8 -- import cProfile import pstats class Success: def __call__(self): pass class Failure: pass a = Success() b = Failure() left_column_width = 35 loop_range = range(5000000) def test1(): for x in loop_range: if hasattr(a, '__call__'): pass def test2(): for x in loop_range: if callable(a): pass def test3(): for x in loop_range: if hasattr(b, '__call__'): pass def test4(): for x in loop_range: if callable(b): 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') test4_profile = cProfile.Profile() test4_profile.runcall(test4) test4_profile.print_stats(sort='cumtime') print('number of loops:'.ljust(left_column_width, ' '), len(loop_range), '\n') print('a hasattr __call__:'.ljust(left_column_width, ' '), pstats.Stats(test1_profile).total_tt) print('a is callable:'.ljust(left_column_width, ' '), pstats.Stats(test2_profile).total_tt, '\n') print('b does not have attr __call__:'.ljust(left_column_width, ' '), pstats.Stats(test3_profile).total_tt) print('b is not callable:'.ljust(left_column_width, ' '), pstats.Stats(test4_profile).total_tt, '\n')