# -- coding: utf-8 -- import cProfile import pstats a = {} b = 0 c = 'Testing' left_column_width = 40 loop_range = range(3000000) def test1(): for i in loop_range: if hasattr(a, 'keys'): a.keys() else: pass def test2(): for i in loop_range: if hasattr(a, 'startswith'): pass else: pass def test3(): for i in loop_range: try: a.keys() except AttributeError: pass def test4(): for i in loop_range: try: a.startswith('Test') except AttributeError: pass def test5(): for i in loop_range: if isinstance(a, dict): a.keys() else: pass def test6(): for i in loop_range: if isinstance(a, str): pass else: pass def test7(): for i in loop_range: if type(a) is dict: a.keys() else: pass def test8(): for i in loop_range: if type(a) is str: pass else: pass def test9(): for i in loop_range: if hasattr(b, 'real'): b.real else: pass def test10(): for i in loop_range: if hasattr(b, 'startswith'): pass else: pass def test11(): for i in loop_range: try: b.real except AttributeError: pass def test12(): for i in loop_range: try: x = b.startswith('Test') except AttributeError: pass def test13(): for i in loop_range: if isinstance(b, int): b.real else: pass def test14(): for i in loop_range: if isinstance(b, str): pass else: pass def test15(): for i in loop_range: if type(b) is int: b.real else: pass def test16(): for i in loop_range: if type(b) is str: pass else: pass def test17(): for i in loop_range: if hasattr(c, 'startswith'): x = c.startswith('Test') else: pass def test18(): for i in loop_range: if hasattr(c, 'keys'): pass else: pass def test19(): for i in loop_range: try: x = c.startswith('Test') except AttributeError: pass def test20(): for i in loop_range: try: x = c.keys() except AttributeError: pass def test21(): for i in loop_range: if isinstance(c, str): x = c.startswith('Test') else: pass def test22(): for i in loop_range: if isinstance(c, dict): pass else: pass def test23(): for i in loop_range: if type(c) is str: x = c.startswith('Test') else: pass def test24(): for i in loop_range: if type(c) is dict: pass else: 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') 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') test22_profile = cProfile.Profile() test22_profile.runcall(test22) test22_profile.print_stats(sort='cumtime') test23_profile = cProfile.Profile() test23_profile.runcall(test23) test23_profile.print_stats(sort='cumtime') test24_profile = cProfile.Profile() test24_profile.runcall(test24) test24_profile.print_stats(sort='cumtime') print('number of loops:'.ljust(left_column_width, ' '), len(loop_range), '\n') # All Tests Pass print("hasattr(a, 'keys'):".ljust(left_column_width, ' '), pstats.Stats(test1_profile).total_tt) print("try/except - a.keys():".ljust(left_column_width, ' '), pstats.Stats(test3_profile).total_tt) print("isinstance(a, dict):".ljust(left_column_width, ' '), pstats.Stats(test5_profile).total_tt) print("type(a) is dict:".ljust(left_column_width, ' '), pstats.Stats(test7_profile).total_tt, '\n') print("hasattr(b, 'real'):".ljust(left_column_width, ' '), pstats.Stats(test9_profile).total_tt) print("try/except - b.real:".ljust(left_column_width, ' '), pstats.Stats(test11_profile).total_tt) print("isinstance(b, int):".ljust(left_column_width, ' '),pstats.Stats(test13_profile).total_tt) print("type(b) is int:".ljust(left_column_width, ' '), pstats.Stats(test15_profile).total_tt, '\n') print("hasattr(c, 'startswith'):".ljust(left_column_width, ' '), pstats.Stats(test17_profile).total_tt) print("try/except - c.startswith('Test'):".ljust(left_column_width, ' '), pstats.Stats(test19_profile).total_tt) print("isinstance(c, str):".ljust(left_column_width, ' '), pstats.Stats(test21_profile).total_tt) print("type(c) is str:".ljust(left_column_width, ' '), pstats.Stats(test23_profile).total_tt, '\n') # All Tests Fail print("hasattr(a, 'startswith'):".ljust(left_column_width, ' '), pstats.Stats(test2_profile).total_tt) print("try/except - a.startswith('Test'):".ljust(left_column_width, ' '), pstats.Stats(test4_profile).total_tt) print("isinstance(a, str):".ljust(left_column_width, ' '), pstats.Stats(test6_profile).total_tt) print("type(a) is str:".ljust(left_column_width, ' '), pstats.Stats(test8_profile).total_tt, '\n') print("hasattr(b, 'startswith'):".ljust(left_column_width, ' '), pstats.Stats(test10_profile).total_tt) print("try/except - b.startswith('Test'):".ljust(left_column_width, ' '), pstats.Stats(test12_profile).total_tt) print("isinstance(b, str):".ljust(left_column_width, ' '), pstats.Stats(test14_profile).total_tt) print("type(b) is str:".ljust(left_column_width, ' '), pstats.Stats(test16_profile).total_tt, '\n') print("hasattr(c, 'keys'):".ljust(left_column_width, ' '), pstats.Stats(test18_profile).total_tt) print("try/except - c.keys():".ljust(left_column_width, ' '), pstats.Stats(test20_profile).total_tt) print("isinstance(c, dict):".ljust(left_column_width, ' '), pstats.Stats(test22_profile).total_tt) print("type(c) is dict:".ljust(left_column_width, ' '), pstats.Stats(test24_profile).total_tt, '\n')