Five Thousand Rows Budget

A request handler has a p99 budget of 500 ms and currently spends 210 ms of it here, on a frame of about 5,000 rows:

df["margin"] = df.apply(lambda r: (r.a * r.b - r.c) / r.d, axis=1)

All four columns are float64. Four proposals are on the table. Which is the right one?

A

Keep apply and decorate the lambda with @numba.njit, so the per-row arithmetic compiles to machine code.

B

df["margin"] = (df.a * df.b - df.c) / df.d. The elementwise NumPy path is the entire fix at this size.

C

df["margin"] = pd.eval("(df.a * df.b - df.c) / df.d"), because eval avoids materializing the intermediate arrays and so beats the plain expression.

D

Keep apply but pass raw=True, so the callback receives a NumPy array per row instead of a freshly constructed Series.