# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [default]
#     language: python
#     name: python3
# ---

import numpy as np
N = int(1e7)
x = np.linspace(-1, 1, N)
# %time y = 0.25 * x**3 + 0.75 * x**2 + 1.5 * x - 2

import numexpr as ne
ne.set_num_threads(1)
# %time y2 = ne.evaluate("0.25 * x**3 + 0.75 * x**2 + 1.5 * x - 2")

np.allclose(y, y2)

# %timeit y = ((0.25 * x + 0.75) * x + 1.5) * x - 2

# %timeit y2 = ne.evaluate("((0.25 * x + 0.75) * x + 1.5) * x - 2")

# What happens with 4 cores?
ne.set_num_threads(4)

# %timeit y3 = ne.evaluate("((0.25 * x + 0.75) * x + 1.5) * x - 2")

# +
import numba

@numba.autojit
def poly(x):
    y = np.empty(N, dtype=np.float64)
    for i in range(N):
        # y[i] = 0.25*x[i]**3 + 0.75*x[i]**2 + 1.5*x[i] - 2
        y[i] = ((0.25*x[i] + 0.75)*x[i] + 1.5)*x[i] - 2
    return y



# -

y3 = poly(x)  # run through Numba!
np.allclose(y, y3)

# %timeit y3 = poly(x)



StarcoderdataJupyterScriptsDedupFiltered