Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
"""
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))
Ejemplo n.º 3
0
@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,
        ),
    )

Ejemplo n.º 4
0

@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)",
Ejemplo n.º 5
0
        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),
    ),
Ejemplo n.º 6
0
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),
)
Ejemplo n.º 7
0
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:",
    timeit.timeit("f(15)", globals=dict(f=jit_fib_fix_untyped), number=10000),
Ejemplo n.º 8
0
    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))