Get began with Python’s new native JIT

Learn extra at:

Right here’s an instance of a program that demonstrates fairly constant speedups with the JIT enabled. It’s a rudimentary model of the Mandelbroit fractal:

from time import perf_counter
import sys

print ("JIT enabled:", sys._jit.is_enabled())

WIDTH = 80
HEIGHT = 40
X_MIN, X_MAX = -2.0, 1.0
Y_MIN, Y_MAX = -1.0, 1.0
ITERS = 500

YM = (Y_MAX - Y_MIN)
XM = (X_MAX - X_MIN)

def iter(c):
    z = 0j
    for _ in vary(ITERS):
        if abs(z) > 2.0:
            return False
        z = z ** 2 + c
    return True

def generate():
    begin = perf_counter()
    output = []

    for y in vary(HEIGHT):
        cy = Y_MIN + (y / HEIGHT) * YM
        for x in vary(WIDTH):
            cx = X_MIN + (x / WIDTH) * XM
            c = complicated(cx, cy)
            output.append("#" if iter(c) else ".")
        output.append("n")
    print ("Time:", perf_counter()-start)
    return output

print("".be part of(generate()))

When this system begins operating, it lets you understand if the JIT is enabled after which produces a plot of the fractal to the terminal together with the time taken to compute it.

With the JIT enabled, there’s a reasonably constant 20% speedup between runs. If the efficiency increase isn’t apparent, strive altering the worth of ITERS to a better quantity. This forces this system to do extra work, so ought to produce a extra apparent speedup.

Leave a reply

Please enter your comment!
Please enter your name here