# -- coding: utf-8 -- import cProfile import pstats a = None b = 'Test' left_column_width = 20 loop_range = range(20000000) def test1(): for i in loop_range: if a == None: pass def test2(): for i in loop_range: if a is None: pass def test3(): for i in loop_range: if a != None: pass def test4(): for i in loop_range: if a is not None: pass def test5(): for i in loop_range: if b == None: pass def test6(): for i in loop_range: if b is None: pass def test7(): for i in loop_range: if b != None: pass def test8(): for i in loop_range: if b is not None: 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') print('number of loops:'.ljust(left_column_width, ' '), len(loop_range), '\n') print('a == None:'.ljust(left_column_width, ' '), pstats.Stats(test1_profile).total_tt) print('a is None:'.ljust(left_column_width, ' '), pstats.Stats(test2_profile).total_tt, '\n') print('a != None:'.ljust(left_column_width, ' '), pstats.Stats(test3_profile).total_tt) print('a is not None:'.ljust(left_column_width, ' '), pstats.Stats(test4_profile).total_tt, '\n') print('b == None:'.ljust(left_column_width, ' '), pstats.Stats(test5_profile).total_tt) print('b is None:'.ljust(left_column_width, ' '), pstats.Stats(test6_profile).total_tt, '\n') print('b != None:'.ljust(left_column_width, ' '), pstats.Stats(test7_profile).total_tt) print('b is not None:'.ljust(left_column_width, ' '), pstats.Stats(test8_profile).total_tt, '\n')