Fast numerical expression evaluator for NumPy

2.14.1 · active · verified Sun Apr 05

NumExpr is a Python library that provides a fast numerical expression evaluator for NumPy. It accelerates array operations by avoiding memory allocation for intermediate results, leading to better cache utilization and reduced memory access. It also leverages multi-threading to utilize multiple CPU cores and supports Intel's Math Kernel Library (MKL) for further performance gains, especially with transcendental functions. The current version is 2.14.1, and it maintains a regular release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import `numexpr` and use its primary `evaluate` function. It takes a string expression, which is then parsed and efficiently computed on large NumPy arrays, leveraging NumExpr's optimizations and multi-threading. The example uses common mathematical and logical operations.

import numpy as np
import numexpr as ne

# Create large arrays for demonstrating performance benefits
a = np.arange(1_000_000, dtype=np.float64)
b = np.arange(1_000_000, 0, -1, dtype=np.float64)

# Evaluate a complex expression using numexpr
result_ne = ne.evaluate("sin(a) + arcsinh(a/b) + (a * b - 4.1 * a) > 2.5 * b")

print(result_ne)
print(f"Result type: {result_ne.dtype}")

# You can also set the number of threads dynamically
# print(f"Current numexpr threads: {ne.nthreads}")
# ne.set_num_threads(4)
# print(f"New numexpr threads: {ne.nthreads}")

view raw JSON →