def bench_guards(nguard): def func(): pass no_guard = bench(func, number=100) print("no guard: %s" % format_dt(no_guard)) if fat.get_specialized(func): print("ERROR: func already specialized") sys.exit(1) guards = [ fat.GuardDict(globals(), ('global_var', )) for i in range(nguard) ] fat.specialize(func, fast_func, guards) with_guards = bench(func) print("with %s guards on globals: %s" % (nguard, format_dt(with_guards))) dt = with_guards - no_guard print("cost of %s guards: %s (%.1f%%)" % (nguard, format_dt(dt), dt * 100 / no_guard)) dt = dt / nguard print("average cost of 1 guard: %s (%.1f%%)" % (format_dt(dt), dt * 100 / no_guard)) print()
def bench_guards(nguard): def func(): pass no_guard = bench(func, number=100) print("no guard: %s" % format_dt(no_guard)) if fat.get_specialized(func): print("ERROR: func already specialized") sys.exit(1) guards = [fat.GuardDict(globals(), ("global_var",)) for i in range(nguard)] fat.specialize(func, fast_func, guards) with_guards = bench(func) print("with %s guards on globals: %s" % (nguard, format_dt(with_guards))) dt = with_guards - no_guard print("cost of %s guards: %s (%.1f%%)" % (nguard, format_dt(dt), dt * 100 / no_guard)) dt = dt / nguard print("average cost of 1 guard: %s (%.1f%%)" % (format_dt(dt), dt * 100 / no_guard)) print()
def main(): if fat.get_specialized(func) or fat.get_specialized(func2): print("ERROR: functions already specialized!") sys.exit(1) fat.specialize(func2, fast_func2, [fat.GuardArgType(0, (list,))]) for range_pow10 in (0, 1, 3, 5): print("range(10 ** %s)" % range_pow10) dt = bench_list('func', range_pow10) print("- original bytecode: %s" % format_dt(dt)) dt2 = bench_list('func2', range_pow10) print("- append=obj.append with guards: %s" % compared_dt(dt2, dt)) dt2 = bench_list('fast_func2', range_pow10) print("- append=obj.append: %s" % compared_dt(dt2, dt))
def main(): if fat.get_specialized(func) or fat.get_specialized(func2): print("ERROR: functions already specialized!") sys.exit(1) fat.specialize(func2, fast_func2, [fat.GuardArgType(0, (list, ))]) for range_pow10 in (0, 1, 3, 5): print("range(10 ** %s)" % range_pow10) dt = bench_list('func', range_pow10) print("- original bytecode: %s" % format_dt(dt)) dt2 = bench_list('func2', range_pow10) print("- append=obj.append with guards: %s" % compared_dt(dt2, dt)) dt2 = bench_list('fast_func2', range_pow10) print("- append=obj.append: %s" % compared_dt(dt2, dt))
import ast import fatoptimizer import time from fatoptimizer.benchmark import format_dt config = fatoptimizer.Config() config.enable_all() filename = 'x.py' tree = ast.parse('x') print("Optimize AST tree:") print(ast.dump(tree)) loops = 1000 best = None for run in range(5): start = time.perf_counter() for loop in range(loops): fatoptimizer.optimize(tree, filename, config) dt = (time.perf_counter() - start) / loops if best is not None: best = min(best, dt) else: best = dt print("%s / call" % format_dt(dt))
"""Test whether a path is absolute""" sep = _get_sep(s) return s.startswith(sep) def fast_isabs(s): """Test whether a path is absolute""" sep = _get_sep(s) return s.startswith(sep) # Manually inline _get_sep() in isabs() depending on the type of the s argument def isabs_str(s): return s.startswith('/') for func in (_get_sep, isabs, fast_isabs, isabs_str): if fat.get_specialized(func): print("ERROR: a function is already specialized!") sys.exit(1) fat.specialize(fast_isabs, isabs_str, [fat.GuardArgType(0, (str,)), fat.GuardGlobals(('_get_sep',)), fat.GuardBuiltins(('isinstance',)), fat.GuardFunc(_get_sep)]) dt = bench("isabs('/abc')") print("original isabs() bytecode: %s" % format_dt(dt)) dt2 = bench("fast_isabs('/abc')") print("_get_sep() inlined in isabs(): %s" % compared_dt(dt2, dt))