def profile_module(callback, *args, **kwargs): p = profile() p.snapshot_stats() p.enable() callback(*args, **kwargs) p.disable() p.print_stats(2)
def erase_stdout(): with file(os.devnull, "w") as null: old_stdout = sys.stdout sys.stdout = null yield sys.stdout = old_stdout def cnfgen_call(): from cnfformula import cnfgen cmd = ["cnfgen"] + sys.argv[1:] with erase_stdout(): cnfgen(cmd) if __name__ == '__main__': from cProfile import run as profile if len(sys.argv) <= 1: print("Usage: {} <cnfgen_args>".format(sys.argv[0]), file=sys.stderr) sys.exit(-1) profile('cnfgen_call()', sort='tottime')
[ 4,52, 8,83,97,35,99,16, 7,97,57,32,16,26,26,79,33,27,98,66], [88,36,68,87,57,62,20,72, 3,46,33,67,46,55,12,32,63,93,53,69], [ 4,42,16,73,38,25,39,11,24,94,72,18, 8,46,29,32,40,62,76,36], [20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74, 4,36,16], [20,73,35,29,78,31,90, 1,74,31,49,71,48,86,81,16,23,57, 5,54], [ 1,70,54,71,83,51,54,69,16,92,33,48,61,43,52, 1,89,19,67,48]] def line_product(x, y, slope): prod = 1 for i in xrange(4): if y >= 20 or x >= 20: break prod *= grid[y][x] y += slope[1] x += slope[0] return prod def execute(): top = 0 for y in xrange(20): for x in xrange(20): for slope in [(1,0),(1,1),(-1,1),(0,1)]: c = line_product(x, y, slope) if c > top: top = c return top if __name__ == "__main__": from cProfile import run as profile profile("print execute()")
cls = Level level_args = (level, options.cp) elif options.ls: cls = LevelSelect level_args = () else: if conf.COMPLETED: cls = LevelSelect level_args = () else: cls = Level level_args = (conf.CURRENT_LEVEL,) if options.profile: # profile from cProfile import run as profile from pstats import Stats t = options.time * conf.FPS[get_backend_id(cls)] fn = options.fn profile("Game({0}, *level_args).run(t)".format(cls.__name__), fn, locals()) Stats(fn).strip_dirs().sort_stats(options.sort_stats).print_stats(options.num_stats) os.unlink(fn) else: # run normally restarting = True while restarting: restarting = False Game(cls, *level_args).run() pg.quit()
def main(): code = """m = Matrix()\nfor i in range(1000):\n\tm.add_item(i)\nfor i in range(1000):\n\tm.pop()""" profile(code, sort=1)
with file(os.devnull,"w") as null: old_stdout = sys.stdout sys.stdout = null yield sys.stdout = old_stdout def cnfgen_call(): from cnfformula import cnfgen cmd = ["cnfgen"] + sys.argv[1:] with erase_stdout(): cnfgen(cmd) if __name__ == '__main__': from cProfile import run as profile if len(sys.argv) <= 1: print("Usage: {} <cnfgen_args>".format(sys.argv[0]),file=sys.stderr) sys.exit(-1) profile('cnfgen_call()',sort='tottime')