def fbenchmark(f, var=[Symbol('x')]): """ Do some benchmarks with f using clambdify, lambdify and psyco. """ global cf, pf, psyf start = time() cf = clambdify(var, f) print('compile time (including sympy overhead): %f s' % ( time() - start)) pf = lambdify(var, f, 'math') psyf = None psyco = import_module('psyco') if psyco: psyf = lambdify(var, f, 'math') psyco.bind(psyf) code = '''for x in (i/1000. for i in range(1000)): f(%s)''' % ('x,'*len(var)).rstrip(',') t1 = Timer(code, 'from __main__ import cf as f') t2 = Timer(code, 'from __main__ import pf as f') if psyf: t3 = Timer(code, 'from __main__ import psyf as f') else: t3 = None print('for x = (0, 1, 2, ..., 999)/1000') print('20 times in 3 runs') print('compiled: %.4f %.4f %.4f' % tuple(t1.repeat(3, 20))) print('Python lambda: %.4f %.4f %.4f' % tuple(t2.repeat(3, 20))) if t3: print('Psyco lambda: %.4f %.4f %.4f' % tuple(t3.repeat(3, 20)))
def fbenchmark(f, var=[Symbol('x')]): """ Do some benchmarks with f using clambdify, lambdify and psyco. """ global cf, pf, psyf start = time() cf = clambdify(var, f) print 'compile time (including sympy overhead): %f s' % (time() - start) pf = lambdify(var, f, 'math') psyf = None psyco = import_module('psyco') if psyco: psyf = lambdify(var, f, 'math') psyco.bind(psyf) code = '''for x in (i/1000. for i in range(1000)): f(%s)''' % ('x,' * len(var)).rstrip(',') t1 = Timer(code, 'from __main__ import cf as f') t2 = Timer(code, 'from __main__ import pf as f') if psyf: t3 = Timer(code, 'from __main__ import psyf as f') else: t3 = None print 'for x = (0, 1, 2, ..., 999)/1000' print '20 times in 3 runs' print 'compiled: %.4f %.4f %.4f' % tuple(t1.repeat(3, 20)) print 'Python lambda: %.4f %.4f %.4f' % tuple(t2.repeat(3, 20)) if t3: print 'Psyco lambda: %.4f %.4f %.4f' % tuple(t3.repeat(3, 20))
def fbenchmark(f, var=[Symbol("x")]): """ Do some benchmarks with f using clambdify, lambdify and psyco. """ global cf, pf, psyf start = time() cf = clambdify(var, f) print "compile time (including sympy overhead): %f s" % (time() - start) pf = lambdify(var, f, "math") psyf = None try: import psyco psyf = lambdify(var, f, "math") psyco.bind(psyf) except ImportError: pass code = """for x in (i/1000. for i in range(1000)): f(%s)""" % ( "x," * len(var) ).rstrip( "," ) t1 = Timer(code, "from __main__ import cf as f") t2 = Timer(code, "from __main__ import pf as f") if psyf: t3 = Timer(code, "from __main__ import psyf as f") else: t3 = None print "for x = (0, 1, 2, ..., 999)/1000" print "20 times in 3 runs" print "compiled: %.4f %.4f %.4f" % tuple(t1.repeat(3, 20)) print "Python lambda: %.4f %.4f %.4f" % tuple(t2.repeat(3, 20)) if t3: print "Psyco lambda: %.4f %.4f %.4f" % tuple(t3.repeat(3, 20))
def test3b_timing_calc(self): """Test Zernike calculation performance with and without cache, print results""" t1 = Timer(""" a=calc_zernike(vec, rad, z_cache) """, """ from zern import calc_zern_basis, fit_zernike, calc_zernike import numpy as np rad = %d nmodes = %d vec = np.random.random(nmodes) z_cache = calc_zern_basis(len(vec), rad) """ % (self.rad, self.nmodes) ) t2 = Timer(""" a=calc_zernike(vec, rad, {}) """, """ from zern import calc_zern_basis, fit_zernike, calc_zernike import numpy as np rad = %d nmodes = %d vec = np.random.random(nmodes) """ % (self.rad, self.nmodes) ) t_cached = min(t1.repeat(2, self.calc_iter))/self.calc_iter t_nocache = min(t2.repeat(2, self.calc_iter))/self.calc_iter print "test3b_timing_calc(): rad=257, nmodes=25 cache: %.3g s/it no cache: %.3g s/it" % (t_cached, t_nocache)
def test2a_timing(self): """Test timing for two functions""" print "test2a_timing(): timings in msec/iter" for sz in self.sz_l[:1]: for spsz in self.spotsz_l: for pos in self.pos_l: setup_str = """ from __main__ import gauss, _gauss_slow import numpy as np sz = (%d,%d) spsz = %g pos = (%d,%d) amp = %g noi = %g """ % ( sz + (spsz,) + pos + (self.amp, self.noi) ) t1 = Timer( """ g=_gauss_slow(sz, spsz, pos, amp, noi) """, setup_str, ) t2 = Timer( """ a=gauss(sz, spsz, pos, amp, noi) """, setup_str, ) t_g1 = 1000 * min(t1.repeat(3, self.niter)) / self.niter t_g2 = 1000 * min(t2.repeat(3, self.niter)) / self.niter print "test2a_timing(): sz:", sz, "g1: %.3g, g2: %.3g, speedup: %.3g" % (t_g1, t_g2, t_g1 / t_g2)
def test2a_timing(self): """Test timing for two functions""" print "test2a_timing(): timings in msec/iter" for sz in self.sz_l[:1]: for spsz in self.spotsz_l: for pos in self.pos_l: setup_str = """ from __main__ import gauss, _gauss_slow import numpy as np sz = (%d,%d) spsz = %g pos = (%d,%d) amp = %g noi = %g """ % (sz + (spsz, ) + pos + (self.amp, self.noi)) t1 = Timer( """ g=_gauss_slow(sz, spsz, pos, amp, noi) """, setup_str) t2 = Timer(""" a=gauss(sz, spsz, pos, amp, noi) """, setup_str) t_g1 = 1000 * min(t1.repeat(3, self.niter)) / self.niter t_g2 = 1000 * min(t2.repeat(3, self.niter)) / self.niter print "test2a_timing(): sz:", sz, "g1: %.3g, g2: %.3g, speedup: %.3g" % ( t_g1, t_g2, t_g1 / t_g2)
def test3b_timing_calc(self): """Test Zernike calculation performance with and without cache, print results""" t1 = Timer( """ a=calc_zernike(vec, rad, z_cache) """, """ from zern import calc_zern_basis, fit_zernike, calc_zernike import numpy as np rad = %d nmodes = %d vec = np.random.random(nmodes) z_cache = calc_zern_basis(len(vec), rad) """ % (self.rad, self.nmodes)) t2 = Timer( """ a=calc_zernike(vec, rad, {}) """, """ from zern import calc_zern_basis, fit_zernike, calc_zernike import numpy as np rad = %d nmodes = %d vec = np.random.random(nmodes) """ % (self.rad, self.nmodes)) t_cached = min(t1.repeat(2, self.calc_iter)) / self.calc_iter t_nocache = min(t2.repeat(2, self.calc_iter)) / self.calc_iter print "test3b_timing_calc(): rad=257, nmodes=25 cache: %.3g s/it no cache: %.3g s/it" % ( t_cached, t_nocache)
def _timeit(*args, **kwargs): if repeat: t = Timer(partial(func, *args, **kwargs)) print 'hello' try: print t.repeat(repeat, number) except TypeError, e: print 'timing decorator: %s' % e
def timeIt(): ntrials = 10 from timeit import Timer timer1 = Timer('pointRequest()', 'from __main__ import pointRequest') print "Point request took on average", sum(timer1.repeat( ntrials, 1)) / ntrials, 'seconds' timer2 = Timer('hyperRequest()', 'from __main__ import hyperRequest') print "Hyper request took on average", sum(timer2.repeat( ntrials, 1)) / ntrials, 'seconds'
def evaluateRunTime(): global SDStr from timeit import Timer for SDL in SDStr: print( SDL) t1 = Timer("Shudu(\"%s\").scanSDL()" % SDL, "from __main__ import Shudu") print( sum(t1.repeat(10, 1))/10) print( "==================================") for SDL in SDStr: SDL.replace("0", ".") print( SDL) t1 = Timer("solve(\"%s\")" % SDL, "from sudoku import solve") print( sum(t1.repeat(10, 1))/10)
def code() -> None: repeat: int = 100 number: int = 1 counts: list = [1, 2, 4, 8] print(f'START TEST') for c in counts: n_t = Timer(f'n_threaded({c})', 'from __main__ import n_threaded') n_repeat = min(n_t.repeat(repeat=repeat, number=number)) show_repeat(f'n_threaded {c}', n_repeat) y_t = Timer(f'y_threaded({c})', 'from __main__ import y_threaded') y_repeat = min(y_t.repeat(repeat=repeat, number=number)) show_repeat(f'y_threaded {c}', y_repeat) print(f'Test {c} ---')
def evaluateRunTime(): global SDStr from timeit import Timer for SDL in SDStr: print(SDL) t1 = Timer("Shudu(\"%s\").scanSDL()" % SDL, "from __main__ import Shudu") print(sum(t1.repeat(10, 1)) / 10) print("==================================") for SDL in SDStr: SDL.replace("0", ".") print(SDL) t1 = Timer("solve(\"%s\")" % SDL, "from sudoku import solve") print(sum(t1.repeat(10, 1)) / 10)
def main(): global sign_with_csplib, sign_with_cprocsp csplib_tmr = Timer('sign_with_csplib()', setup="from __main__ import sign_with_csplib") cmdline_tmr = Timer('sign_with_cprocsp()', setup="from __main__ import sign_with_cprocsp") t_cmdline = min(cmdline_tmr.repeat(number=100, )) t_csplib = min(csplib_tmr.repeat(number=100, )) if os.path.exists(signname): os.unlink(signname) os.unlink(signname + '.sgn') if os.path.exists(bufname): os.unlink(bufname) print t_cmdline, t_csplib
def time_me(self, gc, tst, params): """ Time the test for class gc and its comparison TimingClass """ stmt, t_setup, runs, reps = params # setup = "import networkx as NX\nG=NX.%s()\n" % gc + t_setup G = eval("nx." + gc + "()") cc = graph_type[(G.is_directed(), G.is_multigraph())] compare_setup = ("import networkx as NX\n" "import timingclasses as tc\n" "G=tc.%s()\n" % (cc,)) + t_setup # tgc = Timer(stmt, setup) tcc = Timer(stmt, compare_setup) # t = tgc.repeat(repeat=runs, number=reps) bt = tcc.repeat(repeat=runs, number=reps) return min(t), min(bt)
def run(f): t1 = Timer(lambda: seq_sample(f)) print('Sequential run') print(t1.repeat(repeat=3, number=1)) t1 = Timer(lambda: thread_sample(f)) print('Using threads') print(t1.repeat(repeat=3, number=1)) t1 = Timer(lambda: process_sample(f)) print('Using processes') print(t1.repeat(repeat=3, number=1)) t1 = Timer(lambda: process_sample_group(f)) print('Using processes (grouped data)') print(t1.repeat(repeat=3, number=1))
def get_closest_region(service="ec2", repetitions=1): """ Get the closest region for a particular service based on its average response time. :type service: str :param service: The service to attempt a connection to. By default, this is ``ec2``. :type repetitions: int :param repetitions: The number of measurements to take before calculating an average. """ regions = [ region.name for region in regioninfo.get_regions(service) if "gov" not in region.name and "cn" not in region.name ] latency = {} for region in regions: connection = Timer( "h.request('GET', '/')", "from http.client import HTTPSConnection; h=HTTPSConnection('%s.%s.amazonaws.com')" % (service, region), ) times = connection.repeat(repetitions, 1) avg_latency = sum(times) / float(len(times)) latency[region] = avg_latency logger.info("Average latency to Amazon %s %s is %s" % (service.upper(), region, latency[region])) region = min(latency, key=latency.get) return region
def get_closest_region(service='ec2', repetitions=1): """ Get the closest region for a particular service based on its average response time. :type service: str :param service: The service to attempt a connection to. By default, this is ``ec2``. :type repetitions: int :param repetitions: The number of measurements to take before calculating an average. """ regions = [ region.name for region in regioninfo.get_regions(service) if 'gov' not in region.name and 'cn' not in region.name ] latency = {} for region in regions: connection = Timer( "h.request('GET', '/')", "from http.client import HTTPSConnection; h=HTTPSConnection('%s.%s.amazonaws.com')" % (service, region)) times = connection.repeat(repetitions, 1) avg_latency = sum(times) / float(len(times)) latency[region] = avg_latency logger.info('Average latency to Amazon %s %s is %s' % (service.upper(), region, latency[region])) region = min(latency, key=latency.get) return region
def time_query_using_module(module): # This is largely copied verbatim from the 'timeit' module repeat = 3 number = 10 verbose = True precision = 3 stmt = partial(query_using_greenlets, module) t = Timer(stmt) try: r = t.repeat(repeat, number) except: t.print_exc() return 1 best = min(r) if verbose: print("raw times:", " ".join(["%.*g" % (precision, x) for x in r])) print("%s: %d loops," % (module.__name__, number)) usec = best * 1e6 / number if usec < 1000: print("best of %d: %.*g usec per loop" % (repeat, precision, usec)) else: msec = usec / 1000 if msec < 1000: print("best of %d: %.*g msec per loop" % (repeat, precision, msec)) else: sec = msec / 1000 print("best of %d: %.*g sec per loop" % (repeat, precision, sec))
def make_plot(file_name: str, function_name: str, repeat_number: int, color: str, approximation_function=None, count_edges: bool = False, count_vertices: bool = False, density: float = None, use_polyfit: bool = False, dim: int = 1): with open(file_name, "rb") as graph_file: graphs_list = pickle.load(graph_file) data = {} for graph in graphs_list: function = getattr(graph, function_name) t = Timer(lambda: function()) function_time = t.repeat(repeat_number, 1) function_time = min(function_time) if count_edges is False and count_vertices \ is False: count_vertices = True if count_edges is True and count_vertices \ is True: print("Błąd, nie można liczyć krawędzi \ i wierzchołków jednocześnie") return if count_vertices: data[len(graph.vertices)] = function_time elif count_edges: data[len(graph.edges)] = function_time x_array = [x for x in data] y_array = [] for x in x_array: y_array.append(data[x]) plt.plot(x_array, y_array, '.') if use_polyfit: z = np.polyfit(x_array, y_array, dim) p = np.poly1d(z) plt.plot(x_array, p(x_array), color, label="{}".format(str(function_name))) else: popt, pcov = curve_fit(approximation_function, x_array, y_array) if density is None: plt.plot(x_array, approximation_function(x_array, *popt), color, label="{}".format(str(function_name))) else: plt.plot(x_array, approximation_function(x_array, *popt), color, label="gęstość {}".format(str(density)))
def evaluateRunTime(): global SDStr from timeit import Timer for SDL in SDStr: print( SDL) t1 = Timer("Shudu(\"%s\").scanSDL()" % SDL, "from __main__ import Shudu") print( sum(t1.repeat(10, 1))/10)
def measure(func, n, w): N = 3 t = Timer( '{0}()'.format(func), 'from __main__ import {0},HeavySetItem\nHeavySetItem({1},{2})'.format( func, n, w)) return sum(t.repeat(N, 1)) / N
def time_code(code_builder, *argsets, repeat=None, number=None): """ Arguments: argset : One or more dict of args: {'args': [], 'kwargs': {}} repeat : Number of times to repeat the test. number : Number of code runs per test. """ validate_argsets(*argsets) code = code_builder(*argsets) t = Timer(code, setup='from colr import Colr, color;C = Colr;') codefmt = format_code(code) progress = AnimatedProgress( codefmt, frames=default_frames, show_time=False, ) repeat = repeat or DEFAULT_REPEAT number = number or DEFAULT_NUMBER with progress: if profiler: profiler.run(code) results = t.repeat( repeat=repeat, number=number, ) result = min(results) return code, result
def time_query_using_module(module): # This is largely copied verbatim from the 'timeit' module repeat = 3 number = 10 verbose = True precision = 3 stmt = partial(query_using_greenlets, module) t = Timer(stmt) try: r = t.repeat(repeat, number) except: t.print_exc() return 1 best = min(r) if verbose: print("raw times:", " ".join(["%.*g" % (precision, x) for x in r])) print("%s: %d loops," % (module.__name__, number)) usec = best * 1e6 / number if usec < 1000: print("best of %d: %.*g usec per loop" % (repeat, precision, usec)) else: msec = usec / 1000 if msec < 1000: print("best of %d: %.*g msec per loop" % (repeat, precision, msec)) else: sec = msec / 1000 print("best of %d: %.*g sec per loop" % (repeat, precision, sec))
def time_test(seq, loops, verify=False): orig = seq timings = [] for func in funcs: seq = orig.copy() value = func(seq) if verify else None t = Timer(lambda: func(seq)) result = sorted(t.repeat(3, loops)) timings.append((result, func.__name__, value)) assert seq == orig, "Sequence altered by {}!".format(func.__name__) first = timings[0][-1] timings.sort() for result, name, value in timings: result = ", ".join([format(u, ".5f") for u in result]) print("{:24} : {}".format(name, result)) if verify: # Check that all results are identical bad = [ "%s: %d" % (name, value) for _, name, value in timings if value != first ] if bad: print("ERROR. Value: {}, bad: {}".format(first, ", ".join(bad))) else: print("Value: {}".format(first)) print()
def speed(inst, number=10, repeat=20): timer = Timer(inst, globals=globals()) raw = np.array(timer.repeat(repeat, number=number)) ave = raw.sum() / len(raw) / number mi, ma = raw.min() / number, raw.max() / number print("Average %1.3g min=%1.3g max=%1.3g" % (ave, mi, ma)) return ave
def run_benchmark(Benchmark, context={}, type=TYPE_PROCESS): if len(str(Benchmark).split(".")) > 1: benchmark_key = str(Benchmark).split(".")[1] else: benchmark_key = str(Benchmark) result = {benchmark_key: []} for i in slave_numbers: context['number'] = i for j in query_numbers: context['query_number'] = j benchmark = Benchmark(context) timer = Timer(lambda: benchmark.run()) print "Executing Benchmark %s (%d slaves, %d queries) ..." % ( benchmark_key, i, j) mean_execution_time = np.mean(timer.repeat(repeat=2, number=1)) # print "%d slaves took avg. %f s" % (i, mean_execution_time) result[benchmark_key].append({ 'type': type, 'execution_time': mean_execution_time, 'slaves': i, 'queries': j }) return result
def time_pyinstrument(function, repeats): timer = Timer(stmt=function) p = pyinstrument.Profiler() p.start() result = timer.repeat(number=repeats) p.stop() return result
def time_stmt(stmt='pass', setup='pass', number=0, repeat=3): """Timer function with the same behaviour as running `python -m timeit ` in the command line. :return: elapsed time in seconds or NaN if the command failed. :rtype: float """ t = Timer(stmt, setup) if not number: # determine number so that 0.2 <= total time < 2.0 for i in range(1, 10): number = 10**i try: x = t.timeit(number) except: print(t.print_exc()) return float('NaN') if x >= 0.2: break try: r = t.repeat(repeat, number) except: print(t.print_exc()) return float('NaN') best = min(r) return best / number
def timeit_kernel(kernel: str = 'd00', convolution_size: Tuple[int, int] = (64, 64), image_pixel: Tuple[float, float] = (1.0, 1.0), psf_pixel: Tuple[float, float] = (1.0, 1.0), psf_size: Tuple[int, int] = (25, 25), n_sources: int = 6000, sigma: float = 2.0, timeout: float = 5.0): pos = make_random_positions(n_sources, convolution_size) psf = make_gaussian_psf_lut(psf_size, sigma) convolution = ConvolveLibrary(kernel, convolution_size, image_pixel, psf_pixel, debug=False) convolution.positions = pos convolution.psf = psf convolution.launch() t = Timer("convolution.launch()", globals={'convolution': convolution}) number, elapsed_time = t.autorange() repeat = int(timeout / elapsed_time) if repeat < 1: results = np.asarray([elapsed_time]) / number else: results = np.asarray(t.repeat(repeat=repeat, number=number)) / number return TimeitProfile(mean=float(np.mean(results)), std=float(np.std(results)), repeat=results.size, number=number)
def run_timeit(function): timer = Timer(function) iterations, _ = timer.autorange() raw_timings = timer.repeat(3, iterations) per_iteration_timings_ns = [dt / iterations for dt in raw_timings] best_ns = min(per_iteration_timings_ns) return best_ns * NS_PER_SEC
def main(): args = parse_args() package_versions = get_package_versions() if args.print_package_versions: print(package_versions) images_per_second = defaultdict(dict) libraries = args.libraries data_dir = args.data_dir paths = list(sorted(os.listdir(data_dir))) paths = paths[: args.images] imgs_cv2 = [read_img_cv2(os.path.join(data_dir, path)) for path in paths] imgs_pillow = [read_img_pillow(os.path.join(data_dir, path)) for path in paths] benchmarks = [ HorizontalFlip(), VerticalFlip(), Rotate(), ShiftScaleRotate(), Brightness(), Contrast(), BrightnessContrast(), ShiftRGB(), ShiftHSV(), Gamma(), Grayscale(), RandomCrop64(), PadToSize512(), Resize512(), RandomSizedCrop_64_512(), Posterize(), Solarize(), Equalize(), Multiply(), MultiplyElementwise(), ] for library in libraries: imgs = imgs_pillow if library in ("torchvision", "augmentor", "pillow") else imgs_cv2 pbar = tqdm(total=len(benchmarks)) for benchmark in benchmarks: pbar.set_description("Current benchmark: {} | {}".format(library, benchmark)) benchmark_images_per_second = None if benchmark.is_supported_by(library): timer = Timer(lambda: benchmark.run(library, imgs)) run_times = timer.repeat(number=1, repeat=args.runs) benchmark_images_per_second = [1 / (run_time / args.images) for run_time in run_times] images_per_second[library][str(benchmark)] = benchmark_images_per_second pbar.update(1) pbar.close() pd.set_option("display.width", 1000) df = pd.DataFrame.from_dict(images_per_second) df = df.applymap(lambda r: format_results(r, args.show_std)) df = df[libraries] augmentations = [str(i) for i in benchmarks] df = df.reindex(augmentations) if args.markdown: makedown_generator = MarkdownGenerator(df, package_versions) makedown_generator.print() else: print(df.head(len(augmentations)))
def _check_cache_behaviour(func): from timeit import Timer timer = Timer(func) uncached_result = func() uncached_time = min(timer.repeat(10, number=1)) with named_temporary_directory() as tmpdir, cache(tmpdir): # Prime the cache func() cached_time = min(timer.repeat(10, number=1)) cached_result = func() print "%s with cache: %s" % (func.__name__, cached_time) print "%s without cache: %s" % (func.__name__, uncached_time) return cached_time, uncached_time, cached_result, uncached_result
def bench_code(setup, src, runs=3, number=10): number_cl = 1 number_np = 1 timer_np = Timer(src+"; comm.Barrier()", setup) t_np = min( timer_np.repeat(runs, number_np) ) / number_np return t_np
def bench_code(setup, src, runs=3, number=10): number_cl = 1 number_np = 1 timer_np = Timer(src+"; comm.Barrier()", setup) t_np = min( timer_np.repeat(runs, number_np) ) / number_np return t_np
def speedtest(): print "Adding %d keys without values" % len(data_add) t = Timer("speed_insert(data_add,c_add)", "from __main__ import speed_insert,data_add,c_add") print format_res(t.repeat(REPEAT, number=1)) print "Adding %d keys with values" % len(data_add_val) t = Timer("speed_insert_val(data_add_val,c_val)", "from __main__ import speed_insert_val,data_add_val,c_val") print format_res(t.repeat(REPEAT, number=1)) print "Getting %d keys" % len(data_get) t = Timer("speed_get(data_get,c_val)", "from __main__ import speed_get,data_get,c_val") print format_res(t.repeat(REPEAT, number=1)) print "Autocomp %d keys" % len(data_autocmp) t = Timer("speed_autocmp(data_autocmp,c_val)", "from __main__ import speed_autocmp,data_autocmp,c_val") print format_res(t.repeat(REPEAT, number=1))
def benchmark(stmt, setup="pass"): timer = Timer(stmt, setup) total = 0.0 k = 1 while total < 0.2: total = min(timer.repeat(3, k)) k *= 10 return 10 * total / k
def time_me(self, gc, tst, params): """ Time the test for class gc and its comparison TimingClass """ stmt, t_setup, runs, reps = params # setup = "import networkx as NX\nG=NX.%s()\n" % gc + t_setup G = eval("nx." + gc + "()") cc = graph_type[(G.is_directed(), G.is_multigraph())] compare_setup = ("import networkx as NX\n" "import timingclasses as tc\n" "G=tc.%s()\n" % (cc, )) + t_setup # tgc = Timer(stmt, setup) tcc = Timer(stmt, compare_setup) # t = tgc.repeat(repeat=runs, number=reps) bt = tcc.repeat(repeat=runs, number=reps) return min(t), min(bt)
def _check_cache_behaviour(func): from timeit import Timer timer = Timer(func) uncached_result = func() uncached_time = min(timer.repeat(10, number=1)) with named_temporary_directory() as tmpdir, cache(tmpdir): # Prime the cache func() cached_time = min(timer.repeat(10, number=1)) cached_result = func() print "%s with cache: %s" % (func.__name__, cached_time) print "%s without cache: %s" % (func.__name__, uncached_time) return cached_time, uncached_time, cached_result, uncached_result
def measure_execution_time(func, data_generator, min_n=100, max_n=100000, n_measures=10, number=1, repeat=1, verbose=False): """ Measure the execution time of a function for increasing N. Input: ------ func -- Function of which the execution time is measured. The function is called as func(data), where data is returned by the argument `data_generator` data_generator -- Function returning input data of 'length' N. Input data for the argument `func` is created as `data_generator(N)`. Common data generators are defined in the submodule `big_o.datagen` min_n, max_n, n_measures -- The execution time of func is measured at `n_measures` points between `min_n` and `max_n` (included) number -- Number of times func is called to compute execution time (return the cumulative time of execution) repeat -- Number of times the timing measurement is repeated. The minimum time for all the measurements is kept. verbose -- If True, print measured time for each tested value of n as soon as it is measured Output: ------- n -- List of N's used as input to `data_generator` time -- List of total execution time for each N in seconds """ # we need a wrapper that holds a reference to func and the generated data # for the timeit.Timer object class func_wrapper(object): def __init__(self, n): self.data = data_generator(n) def __call__(self): return func(next(self.data)) # TODO: check that max_n is not larger than max int64 ns = np.linspace(min_n, max_n, n_measures).astype('int64') execution_time = np.empty(n_measures) for i, n in enumerate(ns): timer = Timer(func_wrapper(n)) measurements = timer.repeat(repeat, number) execution_time[i] = np.min(measurements) log(f'{i+1}/{len(ns)} n={n}: {execution_time[i]}', verbose=verbose) return ns, execution_time
def main(): timer = Timer( "deserialize(data)", "from __main__ import deserialize, readObject; data = readObject()") print "deserializing took", sum(timer.repeat(ntrials, 1)) / ntrials, 'seconds' obj = readObject() b = deserialize(obj) printout(b)
def main(): args = parse_args() if args.print_package_versions: print_package_versions() images_per_second = defaultdict(dict) libraries = ['albumentations', 'imgaug', 'torchvision', 'keras'] data_dir = args.data_dir paths = list(sorted(os.listdir(data_dir))) paths = paths[:args.images] imgs_cv2 = [read_img_cv2(os.path.join(data_dir, path)) for path in paths] imgs_pillow = [ read_img_pillow(os.path.join(data_dir, path)) for path in paths ] for library in libraries: imgs = imgs_pillow if library == 'torchvision' else imgs_cv2 benchmarks = [ HorizontalFlip(), VerticalFlip(), Rotate(), ShiftScaleRotate(), Brightness(), Contrast(), BrightnessContrast(), ShiftRGB(), ShiftHSV(), Gamma(), Grayscale(), RandomCrop64(), PadToSize512(), ] pbar = tqdm(total=len(benchmarks)) for benchmark in benchmarks: pbar.set_description('Current benchmark: {} | {}'.format( library, benchmark)) benchmark_images_per_second = None if hasattr(benchmark, library): timer = Timer(lambda: benchmark.run(library, imgs)) run_times = timer.repeat(number=1, repeat=args.runs) benchmark_images_per_second = [ 1 / (run_time / args.images) for run_time in run_times ] images_per_second[library][str( benchmark)] = benchmark_images_per_second pbar.update(1) pbar.close() pd.set_option('display.width', 1000) df = pd.DataFrame.from_dict(images_per_second) df = df.applymap(lambda r: format_results(r, args.show_std)) df = df[libraries] augmentations = [ 'RandomCrop64', 'PadToSize512', 'HorizontalFlip', 'VerticalFlip', 'Rotate', 'ShiftScaleRotate', 'Brightness', 'Contrast', 'BrightnessContrast', 'ShiftHSV', 'ShiftRGB', 'Gamma', 'Grayscale' ] df = df.reindex(augmentations) print(df.head(len(augmentations)))
def test_list(self, iterations, thelist): results = [] time.sleep(1) for test in thelist: testf = eval("self." + test) timer = Timer(testf) result = min(timer.repeat(iterations, number=1)) results.append(result) print result return results
def test_list(self,iterations,thelist): results=[] time.sleep(1) for test in thelist: testf=eval("self."+test) timer=Timer(testf) result=min(timer.repeat(iterations,number=1)) results.append(result) print result return results
def test2_speedtest(self): "Test speed for different reconstruction methods" t1 = Timer(""" a=calc_phasevec(fakewaves, fakemat, method='scalar') """, """import numpy as np from fringe import calc_phasevec sz = (256, 256) rnd = np.random.random fakewaves = [rnd(sz) + rnd(sz)*1j for i in range(4)] fakemat = rnd((np.product(sz), 20))""") t2 = Timer(""" a=calc_phasevec(fakewaves, fakemat, method='scalar') """, """import numpy as np from fringe import calc_phasevec sz = (256, 256) rnd = np.random.random fakewaves = [rnd(sz) + rnd(sz)*1j for i in range(4)] fakemat = rnd((np.product(sz)*2, 20))""") t3 = Timer(""" a=calc_phasevec(fakewaves, fakemat, method='vshwfs', mlagrid=mlagrid, scale=2) """, """import numpy as np from fringe import calc_phasevec sz = (256, 256) sasz = 16 x0arr = np.arange(sz[0]*0.25, (sz[0]-sasz)*0.75, sasz).astype(int) y0arr = np.arange(sz[1]*0.11, (sz[1]-sasz)*0.90, sasz).astype(int) mlagrid = [(x0, x0+sasz, y0, y0+sasz) for x0 in x0arr for y0 in y0arr] rnd = np.random.random fakewaves = [rnd(sz) + rnd(sz)*1j for i in range(4)] fakemat = rnd((np.product(sz), 20))""") print "calc_phasevec(): timing results:" t_scalar = 1e3*min(t1.repeat(2, 10))/10 print "calc_phasevec(): scalar %.3g msec/it" % (t_scalar) t_gradient = 1e3*min(t2.repeat(2, 10))/10 print "calc_phasevec(): gradient %.3g msec/it" % (t_gradient) t_vshwfs = 1e3*min(t3.repeat(2, 10))/10 print "calc_phasevec(): vshwfs %.3g msec/it" % (t_vshwfs)
def measure_execution_time(func, data_generator, min_n=100, max_n=100000, n_measures=10, n_repeats=1, n_timings=1): """ Measure the execution time of a function for increasing N. Input: ------ func -- Function of which the execution time is measured. The function is called as func(data), where data is returned by the argument `data_generator` data_generator -- Function returning input data of 'length' N. Input data for the argument `func` is created as `data_generator(N)`. Common data generators are defined in the submodule `big_o.datagen` min_n, max_n, n_measures -- The execution time of func is measured at `n_measures` points between `min_n` and `max_n` (included) n_repeats -- Number of times func is called to compute execution time (return the cumulative time of execution) n_timings -- Number of times the timing measurement is repeated. The minimum time for all the measurements is kept. Output: ------- n -- List of N's used as input to `data_generator` time -- List of total execution time for each N in seconds """ # we need a wrapper that holds a reference to func and the generated data # for the timeit.Timer object class func_wrapper(object): def __init__(self, n): self.data = data_generator(n) def __call__(self): return func(self.data) # TODO: check that max_n is not larger than max int64 ns = np.linspace(min_n, max_n, n_measures).astype('int64') execution_time = np.empty(n_measures) for i, n in enumerate(ns): timer = Timer(func_wrapper(n)) measurements = timer.repeat(n_timings, n_repeats) execution_time[i] = np.min(measurements) return ns, execution_time
def main(args): x, y = get_random_ints(), get_random_ints() xa = np.array(x) ya = np.array(y) print(len(list_intersect(x, y))) print(len(numba_list_intersect(x, y))) setup = ''' import numpy as np from array import array from perftest.posting_lists import list_intersect, get_random_ints, numba_list_intersect from perftest.posting_lists_cython import intersect_cython x, y = get_random_ints(), get_random_ints() sx, sy = set(x), set(y) xa = np.array(x) ya = np.array(y) xar = array("i", x) yar = array("i", y) ''' tl = Timer('list_intersect(x, y)', setup) tn = Timer('numba_list_intersect(x, y)', setup) ta = Timer('np.intersect1d(xa, ya)', setup) tr = Timer('intersect_cython(xar, yar)', setup) ts = Timer('sx.intersection(sy)', setup) tz = Timer(foobar, setup) print("list intersection:\t %s%s" % (round(min(tl.repeat(5, 10)), 6), "s")) print("numba list intersection:\t %s%s" % (round(min(tn.repeat(5, 10)), 6), "s")) print("numpy array intersection:\t %s%s" % (round(min(ta.repeat(5, 10)), 6), "s")) print("cython array intersection:\t %s%s" % (round(min(tr.repeat(5, 10)), 6), "s")) print("python set intersection:\t %s%s" % (round(min(ts.repeat(5, 10)), 6), "s")) print("python timeit callable:\t %s%s" % (round(min(tz.repeat(5, 10)), 6), "s"))
def test_it(self): docid = 1820790055 with open("paths.pickle") as f: paths = pickle.load(f) with open("_unindex.pickle") as f: _unindex = pickle.load(f) def old(): unindex_paths = [] unin = _unindex[docid] for oldpath in list(unin): if list(oldpath.split('/')) not in paths: unindex_paths.append((docid, (oldpath,))) print "unindex_paths=%s" % unindex_paths def new(): unindex_paths = [] unin = set(_unindex[docid]) paths_set = set(['/'.join(x) for x in paths]) for oldpath in unin - paths_set: unindex_paths.append((docid, (oldpath,))) print "unindex_paths=%s" % unindex_paths log.info("[Old] Running test (2 times)..") timer = Timer(stmt=old) old_results = timer.repeat(2, 1) log.info("[Old] Results:\n%s" % old_results) log.info("[New] Running test (2 times)..") timer = Timer(stmt=new) new_results = timer.repeat(2, 1) log.info("[New] Results:\n%s" % new_results)
def test(stmnt, r, n): print "\n", stmnt #old = Timer(stmnt,'from auval.shmem import SharedVar;var=SharedVar("/desires/heading")') new = Timer(stmnt,'from shm import desires;var=desires.heading') #ores=old.repeat(r,n) #oavg=sum(ores)/r #print "Old: ", oavg nres=new.repeat(r,n) navg=sum(nres)/r print "New: ",navg
def get_closest_region(service='ec2', repetitions=1): regions = [region.name for region in regioninfo.get_regions(service) if 'gov' not in region.name and 'cn' not in region.name] latency = {} for region in regions: connection = Timer("h.request('GET', '/')", "from http.client import HTTPSConnection; h=HTTPSConnection('ec2.%s.amazonaws.com')" % region) times = connection.repeat(repetitions, 1) avg_latency = sum(times)/float(len(times)) latency[region] = avg_latency logger.info('Average latency to Amazon %s %s is %s' % (service.upper(), region, latency[region])) region = min(latency, key=latency.get) return region
def main(sys_argv): args = sys_argv[1:] count = int(args[0]) print(("Benchmarking: %sx" % count)) print() for example in examples: test = make_test_function(example) t = Timer(test,) print((min(t.repeat(repeat=3, number=count)))) print("Done")
def test1_speed(self): """Measure model speed""" t1 = Timer( """ dp7_tim = transit_model_dp7(ph, sr=10.36, ep=5.13, ca=0.03, g=0.875, om=0.654, nmodel=400, method=0, verb=0, plot=0) """, """ from lightcurve import transit_model_dp7 import numpy as np ph = 2.0*np.pi*np.arange(1000)/1000.0 """, ) t1_min = min(t1.repeat(3, 100)) / 100 print u"test1_speed(): transit_model_dp7 %.3g ms/it" % (t1_min * 1000)
def timeit(stmt='pass', setup='pass', number=0, repeat=3): """Timer function with the same behaviour as running `python -m timeit ` in the command line. :return: best elapsed time in seconds or NaN if the command failed. :rtype: float """ if not setup: setup = 'pass' if not stmt: stmt = 'pass' key = (stmt, setup, number, repeat) if key in _CACHE: return _CACHE[key] t = Timer(stmt, setup) if not number: # determine number so that 0.2 <= total time < 2.0 for i in range(1, 10): number = 10**i try: x = t.timeit(number) except: print(t.print_exc()) return float('NaN') if x >= 0.2: break try: r = t.repeat(repeat, number) except: print(t.print_exc()) return float('NaN') result = min(r) / number _CACHE[key] = result return result
def measure(func, setup=None, maxtime=1, bestof=3): """timeit() wrapper which tries to get as accurate a measurement as possible w/in maxtime seconds. :returns: ``(avg_seconds_per_call, log10_number_of_repetitions)`` """ from timeit import Timer from math import log timer = Timer(func, setup=setup or '') number = 1 while True: delta = min(timer.repeat(bestof, number)) maxtime -= delta*bestof if maxtime < 0: return delta/number, int(log(number, 10)) number *= 10
def test3c_timing_fit(self): """Test Zernike fitting performance""" t1 = Timer(""" a=fit_zernike(wf, z_cache, nmodes=nmodes) """, """ from zern import calc_zern_basis, fit_zernike, calc_zernike import numpy as np rad = %d nmodes = %d vec = np.random.random(nmodes) z_cache = calc_zern_basis(len(vec), rad) wf = np.random.random((rad, rad)) """ % (self.rad, self.nmodes) ) t_cached = min(t1.repeat(2, self.fit_iter))/self.fit_iter # Caching should be at least twice as fast as no caching print "test3c_timing_fit(): rad=257, nmodes=25 %.3g sec/it" % (t_cached)
def main(): args = parse_args() run_times = defaultdict(dict) libraries = ['albumentations', 'imgaug', 'torchvision', 'keras'] data_dir = args.data_dir paths = list(sorted(os.listdir(data_dir))) paths = paths[:args.images] imgs_cv2 = [read_img_cv2(os.path.join(data_dir, path)) for path in paths] imgs_pillow = [read_img_pillow(os.path.join(data_dir, path)) for path in paths] for library in libraries: imgs = imgs_pillow if library == 'torchvision' else imgs_cv2 benchmarks = [ HorizontalFlip(), VerticalFlip(), Rotate(), ShiftScaleRotate(), Brightness(), ShiftRGB(), ShiftHSV(), Gamma(), Grayscale(), RandomCrop64(), PadToSize512(), ] pbar = tqdm(total=len(benchmarks)) for benchmark in benchmarks: pbar.set_description('Current benchmark: {} | {}'.format(library, benchmark)) run_time = None if hasattr(benchmark, library): timer = Timer(lambda: benchmark.run(library, imgs)) run_time = timer.repeat(number=1, repeat=args.runs) run_times[library][str(benchmark)] = run_time pbar.update(1) pbar.close() pd.set_option('display.width', 1000) df = pd.DataFrame.from_dict(run_times) df = df.applymap(lambda r: format_results(r, args.show_std)) df = df[libraries] augmentations = ['RandomCrop64', 'PadToSize512', 'HorizontalFlip', 'VerticalFlip', 'Rotate', 'ShiftScaleRotate', 'Brightness', 'ShiftHSV', 'ShiftRGB', 'Gamma', 'Grayscale'] df = df.reindex(augmentations) print(df.head(len(augmentations)))
def test_case(g): subarbres = potential_number_of_calls(g) # càlcul soft # begin_soft = time.time() # cluster_soft_cache(g) # end_soft = time.time() # temps_soft = end_soft - begin_soft # re-soft amb cache # cluster_soft_cache(g) f = lambda: cluster_soft_cache(g) T = Timer(f) repeat, number = 5, 1000 vals = T.repeat(repeat=repeat, number=number) temps_cache = min(vals) / number log.info(temps_cache) return BenchmarkCacheCase.create( subarbres=subarbres, soft=temps_soft, cache=temps_cache, )
def bench(): """Benchmark the speed of itools.abnf using "urlparse.urlsplit" as reference. """ # itools timer = Timer( "parse_uri('%s')" % test_string, "from itools.uri.parsing import parse_uri") a = timer.repeat(5, number=1) a = min(a) * 1000 # stdlib timer = Timer( "urlsplit('%s')" % test_string, "from urlparse import urlsplit") b = timer.timeit(1) b = b * 1000 print '=== urlsplit ===' print 'itools: %0.3f ms' % a print 'stdlib: %0.3f ms' % b print print 'itools %d times slower than stdlib' % (a/b) print