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()
"""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))