pandas str.contains(regex=False) is ~22.22× slower than polars str.contains(literal=True) for substring search over a text column
Both sides are told the pattern is a literal, not a regular expression.
Result
| Run | pandas str.contains(regex=False) | polars str.contains(literal=True) | Ratio |
|---|---|---|---|
| Run 1 | 59.41 | 2.8 | 21.22× |
| Run 2 | 61.42 | 2.61 | 23.51× |
| Run 3 | 81.89 | 3.69 | 22.22× |
Spread over 3 runs: pandas str.contains(regex=False) CV 18.4% / polars str.contains(literal=True) CV 18.9% / ratio CV 5.2%
The two calls
df[df['s'].str.contains('item-001', regex=False)] df.filter(pl.col('s').str.contains('item-001', literal=True)) Conditions
400,000 short strings of the form `item-00123-45`.
Where this number does not apply
A regex pattern costs more on both sides; that comparison is not on this page.
Reproduce
cd bench && python run.py --run 1 --out ../data/run1.json