Skip to content

Benchmarks

TL;DR

Building a model with ten million variables and their constraints took 8.6 s with polarobi and 90.3 s with the fastest alternative, a plain gurobipy loop: 10.5 times faster, with identical models. At a million rows it was 0.8 s against 7.8 s. polarobi was the fastest build at every size, from 1.7 times faster at a thousand rows, and gurobipy-pandas the slowest from a million rows on.

Build Time

Build time against model size, both axes linear, with shaded bands from the fastest to the slowest run: at 10M rows gurobipy-pandas takes 150.7 s, the two plain gurobipy builds 99.4 s and 90.3 s, and polarobi 8.6 s, close to the axis at every size Build time against model size, both axes linear, with shaded bands from the fastest to the slowest run: at 10M rows gurobipy-pandas takes 150.7 s, the two plain gurobipy builds 99.4 s and 90.3 s, and polarobi 8.6 s, close to the axis at every size

Rowsgurobipy, addLConstr loopgurobipy, addConstrsgurobipy-pandaspolarobiSpeedup
13 ms22 ms27 ms8 ms1.7x
run 113 ms22 ms27 ms8 ms
run 213 ms22 ms27 ms7 ms
run 313 ms22 ms26 ms8 ms
70 ms104 ms95 ms18 ms3.8x
run 171 ms103 ms94 ms18 ms
run 269 ms104 ms96 ms19 ms
run 369 ms104 ms95 ms18 ms
614 ms755 ms638 ms70 ms8.8x
run 1610 ms746 ms632 ms71 ms
run 2612 ms797 ms644 ms69 ms
run 3619 ms723 ms638 ms69 ms
7.8 s8.8 s11.0 s762 ms10.2x
run 17.9 s8.8 s10.9 s772 ms
run 27.7 s8.8 s10.9 s762 ms
run 37.8 s8.7 s11.2 s751 ms
90.3 s99.4 s150.7 s8.6 s10.5x
run 190.9 s99.4 s150.9 s8.6 s
run 290.5 s99.4 s149.7 s8.6 s
run 389.6 s99.5 s151.4 s8.6 s

Times are the mean of three runs after an untimed warm-up, for building the whole model including the final update(); expand a size to see each run. The speedup divides the mean of the next fastest build by polarobi's, and hovering it names that build: the gurobipy addLConstr loop at every size. All four builds produced the same Fingerprint, and the same variable and constraint names, at every size.

Where the Time Goes

Time per phase at 10M rows: variables 24.9 s with the gurobipy loop, 24.5 s with addConstrs, 26.2 s with gurobipy-pandas and 4.3 s with polarobi; constraints 65.6 s, 74.9 s, 124.7 s and 4.3 s Time per phase at 10M rows: variables 24.9 s with the gurobipy loop, 24.5 s with addConstrs, 26.2 s with gurobipy-pandas and 4.3 s with polarobi; constraints 65.6 s, 74.9 s, 124.7 s and 4.3 s

Constraints are where the other builds lose most: every term is an entry in a LinExpr, and every constraint a separate Python object on its way to Gurobi. In the plain gurobipy builds each term is also a tuple lookup into a tupledict; addConstrs adds a comparison object per constraint and its own bookkeeping on top. gurobipy-pandas builds its expressions with pandas arithmetic over Series of Var objects and then adds one constraint per row, which made it the slowest build from a million rows on. polarobi groups the terms in polars and hands each family to Gurobi in one call. For variables, all builds create one gurobipy handle per variable, which is half of polarobi's remaining time; see How it Works.

The Model

A typical allocation model, generated from a fixed seed:

  • x: one binary variable per option, an (order, factory, week) combination with a quantity and a value. The row count is the number of options, about four per order, over 50 factories and 52 weeks.
  • assign: every order is chosen at most once.
  • capacity: the quantity per factory and week stays within its capacity.
  • y, moq_min, moq_max: a factory week either produces nothing or at least a minimum quantity, a big-M link through one binary variable per factory week.

The gurobipy builds are written the way such models usually are: iter_rows() into addVars, and group_by().agg() with tupledict lookups into one LinExpr per key. They differ only in how they add the constraints:

  • addLConstr loop: one addLConstr call per constraint.
  • addConstrs: one addConstrs call per family, over a generator of the keys.

Both build each expression once; the also common x.sum(order, "*", "*") per constraint would scan the whole tupledict every time and be far slower.

A third build uses gurobipy-pandas, written as its documentation suggests: gppd.add_vars on frames indexed by their keys, groupby().sum() into one LinExpr per key, and gppd.add_constrs. Its inputs are converted to pandas before the clock starts. It adds variables with one addMVar call, but constraints one addLConstr call per row.

Nothing is solved, so no license size limits apply.

The Machine

The results on this page come from a virtual machine on Google Cloud, so that anyone can rerun them on the same hardware:

  • Machine: c4-highmem-16 in europe-west1-c, an Intel Granite Rapids CPU with one vCPU per core, limited to 4 visible cores, and 124 GB of memory.
  • System: Ubuntu 26.04 minimal (ubuntu-minimal-2604-resolute-amd64), Linux 7.0.0-1014-gcp.
  • Software: Python 3.14.7, polarobi 0.1.6, polars 2.0.0, gurobipy 14.0.0b1, gurobipy-pandas 1.2.2 and pandas 3.0.6.

All its cores are of one type, so the run needs no CPU pinning. From a million rows on, the three runs of every build stayed within 3% of each other; below that, where a run takes milliseconds, within 6%, except for addConstrs at 100,000 rows, which differed by 10%.

Caveats

  • Your mileage may vary. Results will differ across hardware, models, data shapes, and system load—the ratios within a single run are more telling than the absolute times. There is no magic: expect polarobi to be faster where creating millions of small interim Python objects has a noticeable impact on build time, or when a lot of data crosses the Python-to-Gurobi boundary.
  • Model building only. Solving takes the same time either way, since the models are identical.

Reproducing

The benchmark lives in benchmarks/ in the repository:

uv run --group bench python benchmarks/build_model.py --sizes 1000 10000 100000 1000000 10000000
uv run --group bench python benchmarks/plot.py benchmarks/results/results.json

The first command builds the model all four ways at each size, checks that the fingerprints and names match, and writes the timings, machine, package versions and settings to benchmarks/results/<system>.json; the results on this page are ../benchmarks/results/gcp-c4-highmem-16.json. Without --sizes it runs 10K to 10M rows; 10M rows need well over 11 GB of free memory, since gurobipy-pandas holds a pandas copy of the inputs. --repeats and --seed change the run. The second command draws the figures on this page from a result file and prints the table.

--cpus pins the run to the given logical CPUs. On CPUs with performance and efficiency cores, such as Intel's 12th generation and later, pick one thread per performance core: otherwise the operating system moves the run between core types, and identical builds differ by a factor of two.