def profile_lines(self, functions, statement): from line_profiler import LineProfiler import __builtin__ profile = LineProfiler(*functions) # Add the profiler to the builtins for @profile.. # will probably not work for all modules in ecoControl, # as they are already imported before and @profile will then throw an error if 'profile' in __builtin__.__dict__: had_profile = True old_profile = __builtin__.__dict__['profile'] else: had_profile = False old_profile = None __builtin__.__dict__['profile'] = profile try: try: profile.runctx(statement, globals(), locals()) message = '' except SystemExit: message = """*** SystemExit exception caught in code being profiled.""" except KeyboardInterrupt: message = ("*** KeyboardInterrupt exception caught in code being " "profiled.") finally: if had_profile: __builtin__.__dict__['profile'] = old_profile # Trap text output. stdout_trap = StringIO() profile.print_stats(stdout_trap) output = stdout_trap.getvalue() output = output.rstrip() pfile = open("profile.txt", 'a') pfile.write("\n\n" + 20 * "=" + "*********====================== profile at time " + time.strftime("%b %d %Y %H:%M:%S", time.gmtime(time.time())) + "==================================\n\n") pfile.write(output) pfile.close() print '\n*** Profile printout saved to text file profile.txt', message
def raytracer_main(): profiler = 0 if profiler == 0: raytracer_main2() elif profiler == 1: from line_profiler import LineProfiler profile = LineProfiler(_sample_light_sources2, _FaceTextureCache.get_face_texture) profile.runctx('raytracer_main2()', globals(), locals()) profile.print_stats() else: import cProfile cProfile.runctx('raytracer_main2()', globals(), locals(), 'stats5')
def test_WignerDRecursion_lineprofiling(): from line_profiler import LineProfiler ell_max = 8 hcalc = HCalculator(ell_max) cosβ = 2 * np.random.rand(100, 100) - 1 workspace = hcalc.workspace(cosβ) hcalc(cosβ, workspace=workspace) # Run once to ensure everything is compiled profiler = LineProfiler( hcalc.__call__) #, _step_2, _step_3, _step_4, _step_5, _step_6) profiler.runctx('hcalc(cosβ, workspace=workspace)', { 'hcalc': hcalc, 'cosβ': cosβ, 'workspace': workspace }, {}) print() profiler.print_stats()
def profile_lines(self, functions, statement): from line_profiler import LineProfiler import __builtin__ profile = LineProfiler(*functions) # Add the profiler to the builtins for @profile.. # will probably not work for all modules in ecoControl, # as they are already imported before and @profile will then throw an error if 'profile' in __builtin__.__dict__: had_profile = True old_profile = __builtin__.__dict__['profile'] else: had_profile = False old_profile = None __builtin__.__dict__['profile'] = profile try: try: profile.runctx(statement, globals(), locals()) message = '' except SystemExit: message = """*** SystemExit exception caught in code being profiled.""" except KeyboardInterrupt: message = ( "*** KeyboardInterrupt exception caught in code being " "profiled.") finally: if had_profile: __builtin__.__dict__['profile'] = old_profile # Trap text output. stdout_trap = StringIO() profile.print_stats(stdout_trap) output = stdout_trap.getvalue() output = output.rstrip() pfile = open("profile.txt", 'a') pfile.write( "\n\n" + 20 * "=" + "*********====================== profile at time " + time.strftime("%b %d %Y %H:%M:%S", time.gmtime(time.time())) + "==================================\n\n") pfile.write(output) pfile.close() print '\n*** Profile printout saved to text file profile.txt', message
def time_it(f, name="Function", profile=False, line_profile=False): if profile is True or line_profile is True: if line_profile is True: # experimental: import xcsvm.solvers.llw import xcsvm.solvers.ww fs = ( xcsvm.solvers.llw_c.llw_mr_sparse_solver_updates__variant_0, xcsvm.solvers.llw_c.llw_mr_sparse_solver_updates__variant_1, xcsvm.solvers.ww_c.ww_sparse_solver_updates_local__variant_0, xcsvm.solvers.ww_c.ww_sparse_solver_updates_local__variant_1, ) profiler = LineProfiler(*fs) else: profiler = cProfile.Profile() start = time.time() ret = profiler.runctx("f()", globals(), locals()) end = time.time() else: start = time.time() ret = f() end = time.time() msg = ("%s time: %.3f s" % (name, end - start)) if profile is True or line_profile is True: print "*" * 79 print "Profile:" if line_profile is True: profiler.print_stats() else: s = pstats.Stats(profiler) s = s.strip_dirs().sort_stats("time").reverse_order() s.print_stats() print "*" * 79 return ret, msg
print(time.clock() - t0) reset_memoized_states() cProfile.runctx("r=simulation(state,goals='min',terminal='round_end',return_path=False)",globals(),locals(),'sim_stats.prf') s = pstats.Stats('sim_stats.prf') print("return_path=False") s.strip_dirs().sort_stats('time').print_stats() print() print_state(state) print(pd.DataFrame([(card_to_console(x[0]),x[1]) for x in r],columns=('play','scores')).drop_duplicates()) reset_memoized_states() lp = LineProfiler(simulation_step) lp.runctx("r=simulation(state,goals='min',terminal='round_end',return_path=False)",globals(),locals()) lp.print_stats() print_state(state) print(pd.DataFrame([(card_to_console(x[0]),x[1]) for x in r],columns=('play','scores')).drop_duplicates()) # reset_memoized_states() # cProfile.runctx("r2=simulation(state,goals='min',terminal='round_end',return_path=False)",globals(),locals(),'sim_stats.prf') # s = pstats.Stats('sim_stats.prf') # print("return_path=False") # s.strip_dirs().sort_stats('time').print_stats() # print() # t0 = time.clock() # r,c = simulation(state,full=True) # t1 = time.clock() - t0
#!/usr/bin/env python import sys from line_profiler import LineProfiler if __name__ == '__main__': filename = sys.argv[1] profiler = LineProfiler() profiler.runctx( 'execfile(%r, globals())' % (filename,), locals(), locals()) profiler.print_stats()