Skip to content

Instantly share code, notes, and snippets.

@danielytics
Last active August 27, 2021 02:57
Show Gist options
  • Save danielytics/7abb90a0c4d65c1b83b690c61a4b0bf3 to your computer and use it in GitHub Desktop.
Save danielytics/7abb90a0c4d65c1b83b690c61a4b0bf3 to your computer and use it in GitHub Desktop.
std::regex benchmark

Compiled with:

clang++  -O3 benchmark.cpp -o benchmark

Results:

10000000 searches with regex:
Number of ? found: 2200000
std::regex: 0.871037 μs (8710.37 ms total)
10000000 searches without regex:
Number of ? found: 2200000
without regex: 0.00527198 μs (52.7198 ms total)

165.22 times the speed of regex

Uses plf_nanotimer.h for timing.

#include <string>
#include <regex>
#include <iostream>
#include <random>
#include <vector>
#include "plf_nanotimer.h"
bool find_regex (const std::string& input)
{
const static std::regex re("[^?]+\\?.*");
return std::regex_match(input, re);
}
bool find_no_regex (const std::string& input)
{
return input.find('?') != std::string::npos;
}
int main (int argc, char* argv[])
{
const int count = 100000;
std::vector<std::string> inputs{
{"https://www.example.com/"},
{"http://attraction.example.com/"},
{"http://example.org/"},
{"http://www.example.com/?babies=afterthought&brass=basket"},
{"https://example.com/"},
{"https://www.example.org/"},
{"https://www.example.com/?brake=air&ball=baseball"},
{"https://www.example.com/bird"},
{"http://aunt.example.com/arch"},
{"http://anger.example.com/"},
{"https://www.example.org/"},
{"https://blade.example.com/bee/bikes"},
{"http://www.example.com/?bit=advertisement&attraction=border#berry"},
{"https://example.com/"},
{"https://www.example.com/"},
{"http://www.example.com/anger"},
{"https://www.example.com/?afterthought=back&bridge=basketball"},
{"http://bikes.example.com/"},
{"http://example.com/"},
{"https://example.com/blade.php#badge"},
{"http://www.example.com/?arch=boy"},
{"https://aunt.example.net/attraction"},
{"https://www.example.net/bait"},
{"https://www.example.net/bait/ants.php"},
{"https://example.com/action/arm.html"},
{"https://www.example.com/?blade=attraction&berry=basket"},
{"https://example.edu/"},
{"http://www.example.org/bag"},
{"http://example.com/bells/airplane.html?battle=animal&boundary=back#bedroom"},
{"https://example.com/birthday/believe?baby=border#balance"},
{"https://example.com/adjustment"},
{"https://belief.example.com/"},
{"https://example.com/"},
{"http://www.example.com/beef.php"},
{"http://example.com/beginner"},
{"http://www.example.com/"},
{"https://www.example.com/boy"},
{"http://www.example.com/bells#blow"},
{"http://www.example.com/brother/balance.htm"},
{"https://www.example.net/?brake=blow&bite=bait"},
{"http://www.example.com/"},
{"https://base.example.com/#bedroom"},
{"https://www.example.com/"},
{"https://example.com/"},
{"https://example.com/bed.html"},
{"http://www.example.edu/"},
{"https://www.example.org/arch"},
{"https://www.example.com/berry/basketball"},
{"https://www.example.com/"},
{"http://www.example.com/boundary#account"},
{"https://www.example.com/attraction.php?bait=apparatus"},
{"http://babies.example.com/?airport=ants"},
{"https://www.example.com/balance.php"},
{"http://example.com/?bedroom=act&angle=angle"},
{"https://attack.example.com/behavior"},
{"http://www.example.com/?alarm=bed"},
{"https://behavior.example.com/attraction/blood"},
{"http://example.com/aunt/arch.html#basketball"},
{"http://www.example.com/argument.html?ants=airport&activity=brass"},
{"http://base.example.com/bedroom?bite=acoustics&arch=basin"},
{"http://example.com/"},
{"https://www.example.com/bear"},
{"https://www.example.edu/advertisement/breath.html?badge=argument"},
{"https://www.example.com/branch.php?brick=appliance&birth=authority"},
{"https://www.example.com/birds/bait"},
{"http://www.example.net/"},
{"http://www.example.com/beds"},
{"https://www.example.net/angle.php"},
{"https://www.example.com/"},
{"https://example.com/basketball"},
{"http://authority.example.net/baseball"},
{"https://example.com/actor/bath.php"},
{"https://example.com/base/battle"},
{"http://www.example.com/brick.aspx"},
{"http://www.example.com/bedroom/blade.php"},
{"http://example.com/act.php"},
{"https://www.example.org/adjustment/achiever#bike"},
{"https://example.com/"},
{"http://www.example.com/"},
{"http://www.example.com/boy/berry.html"},
{"https://www.example.com/bear"},
{"https://www.example.edu/?bells=act#bat"},
{"https://example.net/"},
{"http://babies.example.com/"},
{"http://example.com/"},
{"http://amusement.example.com/"},
{"https://box.example.com/"},
{"http://example.com/"},
{"http://example.com/?beef=basin"},
{"http://www.example.org/"},
{"http://www.example.com/beds/baby?airplane=berry"},
{"https://www.example.com/advertisement/apparel.php"},
{"http://www.example.com/"},
{"https://bead.example.org/bottle.html?blade=animal"},
{"https://www.example.com/aftermath.html#bells"},
{"http://www.example.org/"},
{"https://bait.example.com/?apparatus=authority"},
{"http://example.com/"},
{"http://www.example.com/advertisement/base.html"},
{"http://www.example.com/brick/border"},
};
unsigned total = (count * inputs.size());
double regex_results;
double no_regex_results;
{
plf::nanotimer timer;
std::cout << total << " searches with regex:\n";
int counter = 0;
timer.start();
for (int i = 0; i < count; i++) {
for (const auto& input : inputs) {
if (find_regex(input)) {
counter++;
}
}
}
regex_results = timer.get_elapsed_us();
std::cout << "Number of ? found: " << counter << "\n";
std::cout << "std::regex: " << (regex_results / total) << " μs (" << (regex_results / 1000) << " ms total)\n";
}
{
plf::nanotimer timer;
std::cout << total << " searches without regex:\n";
int counter = 0;
timer.start();
for (int i = 0; i < count; i++) {
for (const auto& input : inputs) {
if (find_no_regex(input)) {
counter++;
}
}
}
no_regex_results = timer.get_elapsed_us();
std::cout << "Number of ? found: " << counter << "\n";
std::cout << "without regex: " << (no_regex_results / total) << " μs (" << (no_regex_results / 1000) << " ms total)\n";
}
std::cout << "\n";
std::cout << (regex_results / no_regex_results) << " times the speed of regex\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment