Skip to content

Speed up constant * Expr#1185

Open
Zeroto521 wants to merge 6 commits intoscipopt:masterfrom
Zeroto521:expr/__mul__
Open

Speed up constant * Expr#1185
Zeroto521 wants to merge 6 commits intoscipopt:masterfrom
Zeroto521:expr/__mul__

Conversation

@Zeroto521
Copy link
Contributor

As the title. This pr is 1.47x faster than the master branch.

  • Optimized before: 6.5767 seconds
  • Optimized after: 4.4400 seconds
from timeit import timeit

from pyscipopt import Model


m = Model()

n = 1000
x = m.addMatrixVar(n)
e = x.sum()

number = 100_000
cost = timeit(lambda: e * -1.5, number=number)
print(
    f"Cost of multiplying an expression with a number over {number} runs: {cost:.4f} seconds"
)

Replace local prod_v with coef and refactor the numeric-scaling branch to iterate with PyDict_Next instead of a Python dict comprehension. Reuse coef for the product of term coefficients in the Expr * Expr branch and update res assignments accordingly. This simplifies the code and avoids creating intermediate Python dicts during scaling, improving clarity and likely performance.
Add a line to CHANGELOG.md noting a performance improvement: "Speed up `constant * Expr` via C-level API". This documents an optimization that accelerates multiplication of constants by Expr using the C-level API for the upcoming 6.0.0 release.
Extend tests/test_expr.py (test_mul) to cover multiplication of Expr by a scalar and mark Expr*Expr cases. Adds an assertion verifying (x + y) * 2.0 produces the expected string representation and a comment indicating Expr * Expr tests.
Copilot AI review requested due to automatic review settings February 4, 2026 12:00
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the multiplication of a constant with an Expr object by replacing Python-level dictionary comprehension with C-level PyDict_Next iteration, achieving a 1.47x speedup (from 6.58s to 4.44s for the benchmark).

Changes:

  • Replaced dictionary comprehension with C-level API (PyDict_Next) for constant * Expr multiplication
  • Renamed variable prod_v to coef for consistency across multiplication operations
  • Added test coverage for Expr * number multiplication

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/pyscipopt/expr.pxi Optimized __mul__ method to use C-level PyDict_Next API for scalar multiplication; renamed prod_v to coef for consistency
tests/test_expr.py Added test case to verify Expr * number multiplication produces correct results
CHANGELOG.md Documented the performance improvement for constant * Expr operations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Add an assertion in tests/test_expr.py to verify left-side scalar multiplication works: assert str(2.0 * (x + y)) == "Expr({Term(x): 2.0, Term(y): 2.0})". This ensures that multiplying an Expr by a scalar on the left yields the same result and string representation as Expr * scalar.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant