Languages Regex Benchmark

April 1, 2021 · View on GitHub

It's just a simple regex benchmark for different programming languages.

Measures how long it takes to find and count non-overlapping occurrences with default settings.

All benchmarks are wrong, but some are useful - Szilard, benchm-ml

I hope this benchmark can be helpful, but it's not only about performance, but each language also has its engine and offers different features (like UTF support, backreferences, capturing groups ...)

Input text

The input text is a concatenation of Learn X in Y minutes repository.

Maybe isn't the best representative text. I'm searching other texts to add to the benchmark.

Regex patterns

  • Email: [\w\.+-]+@[\w\.-]+\.[\w\.-]+
  • URI: [\w]+://[^/\s?#]+[^\s?#]+(?:\?[^\s#]*)?(?:#[^\s]*)?
  • IPv4: (?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])

The above regex patterns aren't the best or the optimal. The focus is the benchmark, not the matching.

The patterns are applied to the whole file.

Measure

Measuring is done inside the programs to avoid include startup, reading and writing times on results.

Elapsed time include pattern compilation, find and count occurrences.

Performance

Docker image was run on: MacBook Pro (16-inch, 2019), 2.4 GHz Intel Core i9, 32 GB 2667 Mhz DDR4 with macOS Big Sur 11.2.3.

LanguageEmail(ms)URI(ms)IP(ms)Total(ms)
Nim Regex1.3226.927.8436.09
Nim22.7021.496.7550.94
Rust26.6625.705.2857.63
PHP42.8746.305.1794.33
C++ Boost44.9744.1315.13104.23
Javascript59.0047.231.50107.73
Perl94.9263.9620.37179.25
Julia104.5886.555.01196.14
C PCRE2126.10112.1713.10251.37
Crystal128.19112.7013.18254.07
C# .Net Core115.05106.0542.71263.81
Dart104.10107.6476.51288.25
D ldc165.46165.204.85335.51
D dmd187.94189.925.32383.18
Ruby233.88208.8543.14485.86
Python PyPy2158.34139.70253.77551.81
Dart Native278.54307.545.77591.85
Python 2197.92131.74294.42624.08
Kotlin186.20223.05287.49696.74
Java198.33221.87287.81708.01
Python PyPy3258.78221.89257.35738.03
Python 3273.86190.79319.13783.78
Go248.14241.28360.90850.32
C++ STL433.09344.74245.661023.49
C# Mono2859.052431.87145.825436.75

Optimized

The following results are for the optimized version.

LanguageEmail(ms)URI(ms)IP(ms)Total(ms)
Rust11.4311.405.1127.94
Nim Regex1.3725.517.2734.15
Nim22.7921.646.7751.21
C PCRE246.2236.924.7387.87
PHP43.1846.715.2395.12
C++ Boost44.6844.5015.10104.28
Javascript59.2047.671.61108.48
C# .Net Core61.7647.8611.63121.25
Perl96.0063.3920.59179.99
Julia104.3187.985.16197.45
Crystal129.52116.3313.12258.97
Dart105.82107.7878.18291.78
D ldc167.60165.715.07338.37
D dmd187.66192.165.55385.37
Ruby236.93206.5143.70487.14
Python PyPy2161.33143.56258.06562.96
Dart Native273.17306.145.89585.20
Python 2200.54132.89290.26623.69
Kotlin184.13220.31273.76678.21
Java190.74223.77275.24689.75
Python PyPy3268.41226.74261.17756.32
Python 3273.70194.09322.09789.88
Go244.14238.40365.27847.81
C++ STL433.18341.07246.851021.10
C# Mono1400.041189.50145.732735.28
  • Language: Indicates the language.
  • Email(ms), URI(ms), IP(ms): Indicates the time elapsed in milliseconds for finding and counting non-overlapping occurrences for the pattern.
  • Total(ms): Indicates the sum of the above times.

Versions and notes

  • C: gcc 7.5.0 & PCRE2 10.36-2
  • Crystal: crystal 0.35.1 - LLVM: 8.0.0
  • C++: g++ 7.5.0 | Boost 1.65.1.0
  • C#: dotnet 5.0.201 | Mono 6.12.0.122
  • D: DMD v2.089.0 | LDC 1.8.0
  • Dart: Dart 2.12.2
  • Go: go 1.16.2
  • Java: OpenJDK 11.0.10
  • Javascript: node v15.13.0
  • Julia: Julia 1.6.0
  • Kotlin: kotlinc-jvm 1.4.32
  • Nim: Nim 1.4.4
  • Perl: perl v5.26.1
  • PHP: PHP 8.0.3
  • Python: Python 2.7.17 | Python 3.6.9 | PyPy 7.3.3
  • Ruby: ruby 2.5.1p57
  • Rust: rustc 1.51.0 & regex 1.4.5

How to run

The easiest way to run the benchmark is by using Docker.

git clone https://github.com/mariomka/regex-benchmark.git
cd regex-benchmark
docker run --rm -v $(pwd):/var/regex mariomka/regex-benchmark:1.6

Contributing

All contributions are welcome, from tiny optimizations to new implementations.

There are only a few requirements:

  • Follow the style of the current implementations
  • Use the default settings for the regex engine
  • Update Dockerfile if it's necessary

Kudos

License

MIT © Mario Juárez.