def DataReader_bin_test2(self): self.select_file(num=1) prf = LineProfiler() prf.add_function(self.read_bin_file_to_tx2) prf.runcall(self.read_bin_file_to_tx2, start=3 * 10**7, datapoints=10**6) prf.print_stats() print(len(self.x), math.log10((len(self.x)))) self.plot_timecorse_of_move(show_it=1)
def benchmark(cls, func, *args): from line_profiler import LineProfiler prf = LineProfiler() prf.add_function(func) ret = prf.runcall(func, *args) prf.print_stats() return ret
def wrapper(self, f, *args, **kwargs): # memory_profiler with StringIO() as s: rtn = profile(f, stream=s, precision=2)(*args, **kwargs) memory_value = self._memory_profiler_parse(s.getvalue()) # line_profiler prof = LineProfiler() prof.add_function(f) rtn = prof.runcall(f, *args, **kwargs) with StringIO() as s: prof.print_stats(stream=s) mix, line_tmp = self._line_profiler_parse(s.getvalue()) # memory line mix output template = self.L_M_TEMPLATE for l, m in zip(line_tmp, memory_value): l_m_mix = l[:5] + m mix.append(template.format(*l_m_mix)) mix[self.L_M_HEADER_INDEX] = template.format(*self.L_M_HEADER) mix[self.L_M_SEPARATOR_INDEX] += "=" * 27 self.logger.debug("line, memory profiler result\n" + "\n".join(mix)) return rtn
def profile(algo, data=None, to_profile=[]): """ Profile algorithm using line_profiler. :param algo: Algorithm instance. :param data: Stock prices, default is random portfolio. :param to_profile: List of methods to profile, default is `step` method. Example of use: tools.profile(Anticor(window=30, c_version=False), to_profile=[Anticor.weights]) """ from line_profiler import LineProfiler if data is None: data = random_portfolio(n=1000, k=10, mu=0.) to_profile = to_profile or [algo.step] profile = LineProfiler(*to_profile) profile.runcall(algo.run, data) profile.print_stats()
def pytest_runtest_call(item): instrumented = [] if item.get_closest_marker("line_profile"): instrumented += [ import_string(s) for s in item.get_closest_marker("line_profile").args ] if item.config.getvalue("line_profile"): instrumented += [ import_string(s) for s in item.config.getvalue("line_profile") ] if instrumented: lp = LineProfiler(*instrumented) lp.runcall(item.runtest) item.config._line_profile = getattr(item.config, "_line_profile", {}) item.config._line_profile[item.nodeid] = get_stats(lp) else: item.runtest()
def wrapper(self, f, *args, **kwargs): prof = LineProfiler() prof.add_function(f) rtn = prof.runcall(f, *args, **kwargs) with StringIO() as f: prof.print_stats(stream=f) msg = "line_profiler result\n{}".format(f.getvalue()) self.logger.debug(msg) return rtn
class LineProfileStats(DecoratorBase): decorator_name = "add_line_profile" def __init__(self) -> None: super().__init__() self.prof: Optional[LineProfiler] = None if _can_line_profiler: self.prof = LineProfiler() def can_run(self) -> bool: if not _can_line_profiler: warn_import(self.decorator_name, "line_profiler") return False return True def wrapper(self, f, *args, **kwargs): self.prof.add_function(f) rtn = self.prof.runcall(f, *args, **kwargs) return rtn def print_stats(self) -> None: if not self.prof: return None with StringIO() as f: self.prof.print_stats(stream=f) msg = "line_profiler_stats result\n{}".format(f.getvalue()) self.logger.debug(msg) def load(self, delogger) -> None: super().load(delogger) setattr(delogger._logger, "print_stats", self.print_stats)
def _transform_STRIKED(self, match: Match) -> str: if not match['STRIKED_TEXT']: return '' return f"<s>{self._parse(match['STRIKED_TEXT'], 'STRIKED')}</s>" def _transform_SUPERSCRIPT(self, match: Match) -> str: if not match['SUPERSCRIPT_TEXT']: return '' return f"<sup>{self._parse(match['SUPERSCRIPT_TEXT'], 'SUPERSCRIPT')}</sup>" def _transform_SUBSCRIPT(self, match: Match) -> str: if not match['SUBSCRIPT_TEXT']: return '' return f"<sub>{self._parse(match['SUBSCRIPT_TEXT'], 'SUBSCRIPT')}</sub>" def _transform_HORIZ_RULE(self, match: Match) -> str: return f'<hr />' def _post_BACKSLASH_UNESCAPE(self, text: str) -> str: return self._backslash_escape_re.sub(r'\1', text) d = DefaultRenderer() if __name__ == '__main__': from line_profiler import LineProfiler lp = LineProfiler() lp.add_function(d._parse) lp.runcall(d.parse, '*****' * 2000) lp.print_stats() print(d.parse('**__hi__**'))
def main(): N = 10**4 anslist = [0] * 60100 for x in range(1, 100): for y in range(1, 100): for z in range(1, 100): n = f(x, y, z) anslist[n - 1] += 1 for i in range(N): print(anslist[i]) if __name__ == '__name__': main() # print()を画面出力する。 # prof = LineProfiler() # prof.add_function(main) # prof.runcall(main) # prof.print_stats() # print()を画面出力しない。 prof = LineProfiler() prof.add_function(main) with redirect_stdout(open(os.devnull, 'w')): prof.runcall(main) prof.print_stats()
class SpecialTestRunner(SpecialTest): """ Test runner, calls the specified test under specified profiler Mode = None - no profiler, "c" - cProfile, "l" - LineProfiler, "h" - hotshot """ def __init__(self, test, mode=None): super(SpecialTestRunner, self).__init__() self.mode = mode self.test = test self.profiler = None def setup(self): if self.mode == 'c': import cProfile self.profiler = cProfile.Profile() elif self.mode == 'l': from line_profiler import LineProfiler self.profiler = LineProfiler() elif self.mode == 'h': import hotshot self.info['name'] = 'special.prof' self.profiler = hotshot.Profile(self.info['name']) self.test.setup() def run(self): if self.mode == 'c': self.profiler.enable() elif self.mode == 'l': self.profiler.enable_by_count() self.profiler.add_function(Handler.handle) self.profiler.add_function(Condition.check_string_match) self.profiler.add_function(Condition.check_function) self.profiler.add_function(Condition.check_list) t = Timer() # Run itself if self.mode == 'h': self.profiler.runcall(self.test.run) else: self.test.run() print('Test time: %s' % t.delta()) if self.mode == 'c': import pstats import StringIO self.profiler.disable() sio = StringIO.StringIO() ps = pstats.Stats(self.profiler, stream=sio).sort_stats('time') ps.print_stats() print(sio.getvalue()) elif self.mode == 'h': import hotshot.stats print('Processing results...') self.profiler.close() name = self.info['name'] stats = hotshot.stats.load(name) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(50) print('Run "hotshot2calltree -o %s.out %s" to generate the cachegrind file' % (name, name)) elif self.mode == 'l': self.profiler.disable() self.profiler.print_stats()
log_prec_term = nsamples * log(det(precision)) c = (nsamples - 1) * sample_cov + nsamples * outer( sample_mean - mean, sample_mean - mean) # TODO: could add to suffstat data_term = -np.sum(c * precision) ll = .5 * (constant_term + log_prec_term + data_term) return ll if __name__ == '__main__': from conditional_independence import partial_correlation_suffstat from line_profiler import LineProfiler mean = np.zeros(10) cov = np.eye(10) samples = np.random.multivariate_normal(mean, cov, size=100) suffstat = partial_correlation_suffstat(samples) suffstat["samples"] = samples lp = LineProfiler() lp.add_function(gaussian_log_likelihood) def run(): for _ in range(10): a = np.random.normal(size=(20, 10)) gaussian_log_likelihood(suffstat, np.ones(10), a.T @ a) lp.runcall(run) lp.print_stats()
class SpecialTestRunner(SpecialTest): """ Test runner, calls the specified test under specified profiler Mode = None - no profiler, "c" - cProfile, "l" - LineProfiler, "h" - hotshot """ def __init__(self, test, mode=None): super(SpecialTestRunner, self).__init__() self.mode = mode self.test = test self.profiler = None def setup(self): if self.mode == 'c': import cProfile self.profiler = cProfile.Profile() elif self.mode == 'l': from line_profiler import LineProfiler self.profiler = LineProfiler() elif self.mode == 'h': import hotshot self.info['name'] = 'special.prof' self.profiler = hotshot.Profile(self.info['name']) self.test.setup() def run(self): if self.mode == 'c': self.profiler.enable() elif self.mode == 'l': self.profiler.enable_by_count() self.profiler.add_function(Handler.handle) self.profiler.add_function(Condition.check_string_match) self.profiler.add_function(Condition.check_function) self.profiler.add_function(Condition.check_list) t = Timer() # Run itself if self.mode == 'h': self.profiler.runcall(self.test.run) else: self.test.run() print('Test time: %s' % t.delta()) if self.mode == 'c': import pstats import StringIO self.profiler.disable() sio = StringIO.StringIO() ps = pstats.Stats(self.profiler, stream=sio).sort_stats('time') ps.print_stats() print(sio.getvalue()) elif self.mode == 'h': import hotshot.stats print('Processing results...') self.profiler.close() name = self.info['name'] stats = hotshot.stats.load(name) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(50) print( 'Run "hotshot2calltree -o %s.out %s" to generate the cachegrind file' % (name, name)) elif self.mode == 'l': self.profiler.disable() self.profiler.print_stats()
g = cd.rand.directed_erdos(nnodes, .5) g = cd.rand.rand_weights(g) samples = g.sample(nsamples) # corr = np.corrcoef(samples, rowvar=False) # suffstat = dict(C=corr, n=nsamples) def run_hsic_test(): for _ in trange(nruns): i, j = random.sample(list(range(nnodes)), 2) cond_set = random.sample(set(range(nnodes)) - {i, j}, 7) hsic_test(samples, i, j, cond_set) profiler = LineProfiler() profiler.add_function(hsic_test) profiler.runcall(run_hsic_test) profiler.print_stats() # def run_gauss_ci_test(): # for _ in range(nruns): # i, j = random.sample(list(range(nnodes)), 2) # cond_set = set(random.sample(set(range(nnodes)) - {i, j}, 2)) # gauss_ci_test(suffstat, i, j, cond_set) # # # profiler = LineProfiler() # profiler.add_function(gauss_ci_test) # profiler.runcall(run_gauss_ci_test) # profiler.print_stats()
import numpy as np import random np.random.seed(1729) random.seed(1729) nnodes = 15 g = cd.rand.rand_weights(cd.rand.directed_erdos(nnodes, 3 / (nnodes - 1), 1)) iv_node = random nsamples = 100 samples = { frozenset(): g.sample(nsamples), frozenset({iv_node}): g.sample_interventional_perfect({iv_node: cd.GaussIntervention(1, .1)}, nsamples) } corr = np.corrcoef(samples[frozenset()], rowvar=False) suffstat = dict(C=corr, n=nsamples) profiler = LineProfiler() def run_gsp(): for i in range(20): gsp(suffstat, nnodes, gauss_ci_test, nruns=10) profiler.add_function(gsp) profiler.runcall(run_gsp) profiler.print_stats()
#!/usr/bin/env python # -*- coding: utf-8 -*- from application import Application from line_profiler import LineProfiler from cell import * from cell_manager import * def main(): app = Application() app.initialize() for y in range(20): for x in range(20): app.cellManager.set_alive(x, y, True) app.cellManager.next_generation() app.window.quit() profile = LineProfiler() profile.add_module(Cell) profile.add_module(CellManager) profile.add_function(main) profile.runcall(main) profile.print_stats()
a = [p.num_edges for p in pdags] def init_new(): return list( tqdm((cd.PDAG(arcs=arcs, edges=edges, new=True) for arcs, edges in arcs_edges), total=ngraphs)) def init(): return list( tqdm((cd.PDAG(arcs=arcs, edges=edges, new=False) for arcs, edges in arcs_edges), total=ngraphs)) lp = LineProfiler() NEW = False if NEW: lp.add_function(cd.PDAG._add_arcs_from) lp.runcall(init_new) else: lp.add_function(cd.PDAG._add_arc) lp.runcall(init) lp.print_stats() # cpdags1 = list(tqdm((dag.cpdag() for dag in dags), total=ngraphs)) # equal = [cpdag1 == cpdag2 for cpdag1, cpdag2 in zip(cpdags1, cpdags2)] # print(all(equal))
print('to undirected, then to_complete_pdag_new') cpdags1 = list(tqdm((dag.cpdag_new(new=True) for dag in dags), total=ngraphs)) print('to undirected, then to_complete_pdag') cpdag2 = list(tqdm((dag.cpdag_new(new=False) for dag in dags), total=ngraphs)) print('remove unprotected') cpdag3 = list(tqdm((dag.cpdag() for dag in dags), total=ngraphs)) def compute_cpdags_new(): return list(tqdm((dag.cpdag_new(new=True) for dag in dags), total=ngraphs)) def compute_cpdags(): return list(tqdm((dag.cpdag_new(new=False) for dag in dags), total=ngraphs)) lp = LineProfiler() NEW = True if NEW: lp.add_function(cd.DAG.cpdag_new) lp.runcall(compute_cpdags_new) else: lp.add_function(cd.DAG.cpdag_new) lp.runcall(compute_cpdags) lp.print_stats() # cpdags1 = list(tqdm((dag.cpdag() for dag in dags), total=ngraphs)) # equal = [cpdag1 == cpdag2 for cpdag1, cpdag2 in zip(cpdags1, cpdags2)] # print(all(equal))
from crowddynamics.examples.collective_motion import FourExits from crowddynamics.logging import setup_logging from crowddynamics.simulation.agents import ThreeCircle, Circular # TODO: https://nvbn.github.io/2017/05/29/complexity/ def main(simulation, iterations: int, **kwargs): hallway = simulation(**kwargs) hallway.update() for i in range(iterations - 1): hallway.update() if __name__ == '__main__': setup_logging() kw = dict(simulation=FourExits, iterations=100, size_active=10, size_herding=190, agent_type=Circular) profiler = LineProfiler(main) profiler.runcall(main, **kw) profiler.print_stats() # prof = memory_profiler.LineProfiler(backend='psutil') # prof(main)(**kw) # memory_profiler.show_results(prof, precision=1)
(1) 1부터 N까지의 정수 중에서 소수를 찾는다 (2) 1부터 N까지의 정수의 합을 계산한다 """ # (1) out = [] append = out.append for k in range(1, n+1): if is_prime(k): append(k) # (2) a = mysum(n) return [out, a] def task2(n): """ 1부터 N까지의 sqrt()를 계산한다 """ return np.sqrt(np.arange(1, n+1)) def main(): task1(100000) # 부하가 큰 계산 task2(100000) # 부하가 작은 계산 if __name__ == '__main__': # (3) from line_profiler import LineProfiler prf = LineProfiler() prf.add_function(is_prime) prf.runcall(is_prime, 999) prf.print_stats()
break if p == -1: print("NA") else: print(p) def main(): N = 1000001 temp = [True] * (N + 1) temp[0] = temp[1] = False for i in range(2, int((N + 1)**0.5) + 1): if temp[i]: temp[i + i::i] = [False] * (len(temp[i + i::i])) while True: n, price = map(int, input().split()) if n == 0 and price == 0: return ps = [] for i in range(n): ps.append(int(input())) solve(price, ps, temp) prf = LineProfiler() prf.add_function(main) prf.runcall(main) prf.print_stats() main()
g = cd.rand.rand_weights(g) samples = g.sample(nsamples) corr = np.corrcoef(samples, rowvar=False) suffstat = dict(C=corr, n=nsamples) def run_hsic_test(): for _ in range(nruns): i, j = random.sample(list(range(nnodes)), 2) cond_set = random.sample(set(range(nnodes)) - {i, j}, 2) hsic_test(samples, i, j, cond_set) profiler = LineProfiler() profiler.add_function(hsic_test) profiler.runcall(run_hsic_test) profiler.print_stats() def run_gauss_ci_test(): for _ in range(nruns): i, j = random.sample(list(range(nnodes)), 2) cond_set = set(random.sample(set(range(nnodes)) - {i, j}, 2)) gauss_ci_test(suffstat, i, j, cond_set) profiler = LineProfiler() profiler.add_function(gauss_ci_test) profiler.runcall(run_gauss_ci_test) profiler.print_stats()
from line_profiler import LineProfiler LEN = 10000 # ひどいコード def hoge(): a = [] for i in range(LEN): a.append(i * i) a.append(i * i) a.append(i * i) a.append(i * i * i * i * i * i) return a prof = LineProfiler() prof.add_function(hoge) prof.runcall(hoge) prof.print_stats()
def runDTW(self): func = lambda x1, y1: ln.norm(x1 - y1, ord=1) #差の絶対値を関数として定義 dist, cost, acc, path = dtw(self.targetData, self.modelData, dist=func) self.distance = dist if __name__ == "__main__": from line_profiler import LineProfiler A = np.array([0, 3, 3, 3, 5, 5, 2, 2, 1, 1, 1, 2]).reshape(-1, 1) B = np.array([0, 3, 3, 5, 5, 5, 5, 5, 5, 2, 2, 2, 1, 1, 1, 1, 1]).reshape(-1, 1) ctd = CalcTimeserieseDistance() n1 = ctd.generateLabel(300) n2 = ctd.generateLabel(300) A = n1.reshape(-1, 1) B = n2.reshape(-1, 1) ctd.setTargetData(A) ctd.setModelData(B) ctd.runDTW() print(ctd.distance) prf = LineProfiler() prf.add_function(ctd.runDTW) prf.runcall(ctd.runDTW) prf.print_stats()
from line_profiler import LineProfiler import causaldag as cd from causaldag.inference.structural import pcalg, skeleton import numpy as np from causaldag.utils.ci_tests import MemoizedCI_Tester, gauss_ci_suffstat, gauss_ci_test import random np.random.seed(1729) random.seed(1729) nnodes = 20 nodes = set(range(nnodes)) g = cd.rand.rand_weights(cd.rand.directed_erdos(nnodes, 3/(nnodes-1), 1)) iv_node = random nsamples = 1000 samples = g.sample(nsamples) suffstat = gauss_ci_suffstat(samples) profiler = LineProfiler() def run_pc(): for i in range(100): ci_tester = MemoizedCI_Tester(gauss_ci_test, suffstat) pcalg(nodes, ci_tester, max_cond_set=None, verbose=True) profiler.add_function(pcalg) profiler.runcall(run_pc) profiler.print_stats()
TEST_CORRECTNESS = False if TEST_CORRECTNESS: om = fadcor_test_vector(samples[:, 0], samples[:, 1]) np.save('test.npy', samples) PROFILE_DYAD = False if PROFILE_DYAD: from line_profiler import LineProfiler def run_dyad_update(): for perm, samples in tqdm(zip(perms, samples_list), total=ntrials): d1 = dyad_update(perm, samples) profiler = LineProfiler() profiler.add_function(dyad_update) profiler.runcall(run_dyad_update) profiler.print_stats() PROFILE_FADCOR = False if PROFILE_FADCOR: from line_profiler import LineProfiler samples_list = [d.sample(nsamples) for _ in range(ntrials)] def run_fadcor(): for samples in tqdm(samples_list): d1 = fadcor_test_vector(samples[:, 0], samples[:, 1]) profiler = LineProfiler() profiler.add_function(fadcor_test_vector) profiler.add_function(partial_sum2d) profiler.add_function(dyad_update)