def run(self, p, use_jit: bool): if use_jit: _run_jit = jit.spec_call( _run, jit.oftype(list), jit.oftype(Tape), jit.oftype(Printer), print_dio_ir=print, ) _run_jit(self.ops, Tape(), p) else: _run(self.ops, Tape(), p)
""" pure py: 2.6820188 jit: 0.8471317999999997 """ import diojit as jit from inspect import getsource import timeit from diojit.runtime.julia_rt import splice, jl_eval print('trans'.center(50, '=')) @jit.jit def f(x): x = 1 + x y = 1 + x z = 1 + y x = 1 + z y = 1 + x z = 1 + y x = 1 + z return x jit_f = jit.spec_call(f, jit.oftype(int)) print(jit_f(10)) print('pure py:', timeit.timeit("f(10)", globals=dict(f=f), number=111111111)) print('jit:', timeit.timeit("f(10)", globals=dict(f=jit_f), number=111111111))
@jit.eagerjit def sum_chain(n: Node): a = 0 while n is not None: a += n.val n = n.next return a n = Node(None, 0) for i in range(100): n = Node(n, (i + 2) * 5) jit_sum_chain = jit.spec_call( sum_chain, jit.oftype(Node), print_dio_ir=print ) print(jit_sum_chain(n), sum_chain(n)) def bench(kind, f, number=1000000): print( kind, timeit.timeit( "f(x)", globals=dict(f=f, x=n), number=number, ), )
@jit.jit def append3(xs, x): xs.append(x) xs.append(x) xs.append(x) print("append3".center(70, "=")) print(getsource(append3)) # jit.In_Def.UserCodeDyn[append3].show() jit_append3 = jit.spec_call( append3, jit.oftype(list), jit.Top, # print_dio_ir=print, ) x = [1] y = 2 # jl_eval(f"println(J_append3_0({splice(x)}, {splice(y)}))") # raise xs = [1] jit_append3(xs, 3) print("test jit func: [1] append 3 for 3 times =", xs) xs = [] print( "append3 (jit) bench time:", timeit.timeit("f(xs, 1)", globals=dict(f=jit_append3, xs=xs),
else: seq.append(line[:-1]) show(out_io, seq) return out_io # @jit.jit # def b(): # return [print, set, list] # for i in range(1000): # print(jit.jit_spec_call(b)()) # b() jit_main = jit.spec_call( main2, jit.oftype(bytearray), # print_dio_ir=print, ) # raise # x = bytearray() jl_eval(f"J_main2_0({splice(x)})") # raise print(main(bytearray()) == jit_main(bytearray())) print( 'jit', timeit.timeit( "main(bytearray())", number=200000, globals=dict(main=jit_main), ),
def fib(a): if a <= 2: return 1 return fib(a - 1) + fib(a - 2) @jit.jit(fixed_references=["fib_fix"]) def fib_fix(a): if a <= 2: return 1 return fib_fix(a + -1) + fib_fix(a + -2) jit_fib_fix_typed = jit.spec_call( fib_fix, jit.oftype(int), # print_jl=print, ) jit_fib_fix_untyped = jit.spec_call(fib_fix, jit.Top) jl_eval(f"println(J_fib__fix_1({splice(20)}))") # check_jl_err(libjl) print("fib".center(70, "=")) print(getsource(fib)) print( "fib(15), jit_fib_fix_untyped(15), jit_fib_fix_typed(15) = ", (fib(15), jit_fib_fix_untyped(15), jit_fib_fix_typed(15)), ) print( "fib(py) bench time:", timeit.timeit("f(15)", globals=dict(f=fib), number=100000), )
print(getsource(hypot)) # print("Direct Translation From Stack Instructions".center(70, "=")) # jit.absint.In_Def.UserCodeDyn[hypot].show() # print("After JITing".center(70, "=")) jit_func_name = repr( jit.spec_call_ir(hypot, jit.S(int), jit.S(int)).e_call.func) hypot_spec = jit.spec_call( hypot, jit.oftype(int), jit.oftype(int), # print_jl=print, # print_dio_ir=print, ) # # # libjl = jit.runtime.julia_rt.get_libjulia() # libjl.jl_eval_string(f'using InteractiveUtils;@code_llvm {jit_func_name}(PyO.int, PyO.int)'.encode()) # jit.runtime.julia_rt.check_jl_err(libjl) print("hypot(1, 2) (jit) = ", hypot_spec(1, 2)) print("hypot(1, 2) (pure py) = ", hypot(1, 2)) print( "hypot (pure py) bench time:", timeit.timeit("f(1, 2)", number=10000000, globals=dict(f=hypot)), ) print(
i = 0 while i < n: min_i = argmin(xs, i, n) swap(xs, min_i, i) i = i + 1 return xs @jit.jit def mwe(xs): return xs[0] < xs[2] jit_msort = jit.spec_call( msort, jit.oftype(list), # print_dio_ir=print, ) xs = list(np.random.randint(0, 10000, 100)) print( "pure py:", timeit.timeit("f(xs)", globals=dict(xs=xs, f=msort), number=100000), ) print( "jit:", timeit.timeit( "f(xs)", globals=dict(xs=xs, f=jit_msort), number=100000 ),
def fib(a): if a <= 2: return 1 return fib(a - 1) + fib(a - 2) @jit.jit(fixed_references=["fib_fix"]) def fib_fix(a): if a <= 2: return 1 return fib_fix(a + -1) + fib_fix(a + -2) jit_fib_fix_untyped = jit.spec_call(fib_fix, jit.Top) jit_fib_fix_typed = jit.spec_call(fib_fix, jit.oftype(int)) # jl_eval(f"println(J_fib__fix_1({splice(20)}))") # check_jl_err(libjl) print("fib".center(70, "=")) print(getsource(fib)) print( "fib(15), jit_fib_fix_untyped(15), jit_fib_fix_typed(15) = ", (fib(15), jit_fib_fix_untyped(15), jit_fib_fix_typed(15)), ) print( "fib(py) bench time:", timeit.timeit("f(15)", globals=dict(f=fib), number=10000), ) print( "fib(jit+untyped) bench time:",
if a.is_literal() and b.is_literal(): const = jit.S(operator.__eq__(a.base, b.base)) ret_types = (jit.S(type(const.base)), ) return jit.CallSpec(const, const, ret_types) return self.spec(jit.S(operator.__eq__), "__call__", list(args)) @jit.eagerjit def fib(x): if x <= 2: return 1 return fib(x + -1) + fib(x + -2) jit_fib = jit.spec_call(fib, jit.oftype(int)) @jit.eagerjit def try_const_fib(x): if try_const_le(x, 2): return 1 return try_const_add( try_const_fib(try_const_add(x, -1), ), try_const_fib(try_const_add(x, -2), ), ) try_const_fib = jit.spec_call(try_const_fib, jit.ofval(20))