Home

Awesome

replace-benchmark

Benchmarks for replace-megaparsec and replace-attoparsec.

Usage

To run the benchmarks,

nix run github:jamesdbrock/replace-benchmark

Method

These benchmarks are intended to measure the wall-clock speed of everything except the actual pattern-matching. Speed of the pattern-matching is the responsibility of the megaparsec and attoparsec libraries.

The benchmark task is to find all of the one-character patterns x in a text stream and replace them by a function which returns the constant string oo. So, like the regex s/x/oo/g.

We have two benchmark input cases, which we call dense and sparse.

The dense case is ten megabytes of alternating spaces and xs like

x x x x x x x x x x x x x x x x x x x x x x x x x x x x

The sparse case is ten megabytes of spaces with a single x in the middle like

                         x

Each benchmark program reads the input from stdin, replaces x with oo, and writes the result to stdout. The time elapsed is measured in milliseconds by perf stat, and the best observed time is recorded.

Results

In milliseconds. Smaller is better.

Function replacement

Here is a comparison of replacement methods which can use an arbitrary function to calculate the replacement string.

Programdense mssparse ms
Python 3.10.10 re.sub repl function570.4236.24
Perl v5.36.0 s///ge function1248.5913.33
Replace.Megaparsec.streamEdit String3099.493020.84
Replace.Megaparsec.streamEdit ByteString3930.55774.05
Replace.Megaparsec.streamEdit Text4104.92916.76
Replace.Attoparsec.ByteString.streamEdit3206.51181.80
Replace.Attoparsec.Text.streamEdit3229.30310.53
Replace.Attoparsec.Text.Lazy.streamEdit3252.55251.83
Text.Regex.Applicative.replace String14366.054633.66
Text.Regex.PCRE.Heavy.gsub Text119.02
Control.Lens.Regex.ByteString.match119.56
Control.Lens.Regex.Text.match36.91

Constant replacement

For reference, here is a comparison of replacement methods which can only replace with a constant string or a templated string.

Programdense mssparse ms
GNU sed 4.9432.5521.18
Python 3.10.10 re.sub repl string267.2836.37
Perl v5.36.0 s///g220.6811.89
Data.ByteString.Search.replace848.2711.19
Data.Text.replace549.2699.93