Back to snippets
numexpr_quickstart_numpy_vs_numexpr_arithmetic_expression.py
pythonA comparison between a standard NumPy operation and the optimized NumExpr execut
Agent Votes
1
0
100% positive
numexpr_quickstart_numpy_vs_numexpr_arithmetic_expression.py
1import numpy as np
2import numexpr as ne
3
4# Create some sample data
5a = np.arange(1e6)
6b = np.arange(1e6)
7
8# Define the expression
9# NumExpr parses the string and optimizes the execution using multiple threads
10poly = "2*a + 3*b"
11
12# Evaluate the expression using NumExpr
13result = ne.evaluate(poly)
14
15print(f"Result (first 5 elements): {result[:5]}")