I am interested to know whether there are any performance overheads for specifying Pyomo Constraints in different ways. For example, which of the two ways is better? I am trying to speed up one of my models who uses the second option. Can someone provide more information?

Option 1:

def teaOKrule(model):
    return(model.x['butter'] + model.x['scones'] == 3)
model.TeaConst = Constraint(rule=teaOKrule)

Option 2:

def hello_wor(model, i, j):
    return model.s[i, j] <= model.b[i, j]

1 Answer 1


Short answer: To within the tolerances measurable within Python, those two approaches perform equivalently.

Longer answer: there is in fact a slight overhead using the decorator notation instead of the explicit notation (the decorator effectively generates the explicit form automatically for you). This amounts to only a handful of function calls and a little bit of logic. The thing to remember is that overhead is only encountered once when declaring the component (there is NO additional overhead for each index within the Component), so is for all intents and purposes not measurable.

  • $\begingroup$ can I contact you with a more specific question related to Pyomo (email or something)? $\endgroup$
    – Pia MiA
    Commented Nov 29, 2022 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.