Created
April 22, 2016 05:24
-
-
Save limingjie/ae5fbacf121a736e1ab157eca2474496 to your computer and use it in GitHub Desktop.
regex perf test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ g++ -Wall -O2 -std=c++11 -o regex_test_perf regex_test_perf.cpp stopwatch.hpp | |
$ ./regex_test_perf.exe | |
[regex e1 ] resume... | |
[regex e1 ] paused. 0m00.306s / 306.031 x 1/1000s | |
[regex e2 ] resume... | |
[regex e2 ] paused. 0m00.144s / 144.014 x 1/1000s | |
[string find ] resume... | |
[string find ] paused. 0m00.014s / 14.001 x 1/1000s | |
[regex e1 ] resume... | |
[regex e1 ] paused. 0m00.601s / 601.061 x 1/1000s | |
[regex e2 ] resume... | |
[regex e2 ] paused. 0m00.283s / 283.028 x 1/1000s | |
[string find ] resume... | |
[string find ] paused. 0m00.028s / 28.003 x 1/1000s | |
[regex e1 ] resume... | |
[regex e1 ] paused. 0m00.900s / 900.091 x 1/1000s | |
[regex e2 ] resume... | |
[regex e2 ] paused. 0m00.432s / 432.043 x 1/1000s | |
[string find ] resume... | |
[string find ] paused. 0m00.041s / 41.004 x 1/1000s | |
[regex e1 ] resume... | |
[regex e1 ] paused. 0m01.215s / 1215.122 x 1/1000s | |
[regex e2 ] resume... | |
[regex e2 ] paused. 0m00.578s / 578.057 x 1/1000s | |
[string find ] resume... | |
[string find ] paused. 0m00.054s / 54.005 x 1/1000s | |
[regex e1 ] resume... | |
[regex e1 ] paused. 0m01.505s / 1505.151 x 1/1000s | |
[regex e2 ] resume... | |
[regex e2 ] paused. 0m00.714s / 714.070 x 1/1000s | |
[string find ] resume... | |
[string find ] paused. 0m00.067s / 67.006 x 1/1000s | |
[regex e1 ] resume... | |
[regex e1 ] paused. 0m01.795s / 1795.180 x 1/1000s | |
[regex e2 ] resume... | |
[regex e2 ] paused. 0m00.853s / 853.084 x 1/1000s | |
[string find ] resume... | |
[string find ] paused. 0m00.080s / 80.007 x 1/1000s | |
[regex e1 ] resume... | |
[regex e1 ] paused. 0m02.082s / 2082.208 x 1/1000s | |
[regex e2 ] resume... | |
[regex e2 ] paused. 0m00.994s / 994.098 x 1/1000s | |
[string find ] resume... | |
[string find ] paused. 0m00.093s / 93.008 x 1/1000s | |
[regex e1 ] resume... | |
[regex e1 ] paused. 0m02.374s / 2374.237 x 1/1000s | |
[regex e2 ] resume... | |
[regex e2 ] paused. 0m01.134s / 1134.112 x 1/1000s | |
[string find ] resume... | |
[string find ] paused. 0m00.106s / 106.009 x 1/1000s | |
[regex e1 ] resume... | |
[regex e1 ] paused. 0m02.663s / 2663.266 x 1/1000s | |
[regex e2 ] resume... | |
[regex e2 ] paused. 0m01.274s / 1274.126 x 1/1000s | |
[string find ] resume... | |
[string find ] paused. 0m00.119s / 119.010 x 1/1000s | |
[regex e1 ] resume... | |
[regex e1 ] paused. 0m02.962s / 2962.296 x 1/1000s | |
[regex e2 ] resume... | |
[regex e2 ] paused. 0m01.423s / 1423.141 x 1/1000s | |
[string find ] resume... | |
[string find ] paused. 0m00.132s / 132.012 x 1/1000s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <regex> | |
#include "stopwatch.hpp" | |
int main() | |
{ | |
std::string str("employee address:5432, 5F, build #1, some drive, state, city, zip"); | |
std::regex e1 ("(.+):(.+)"); | |
std::regex e2 ("([^:]+):([^:]+)"); | |
std::smatch sm; | |
std::string key; | |
std::string value; | |
size_t pos; | |
stopwatch<double> time_e1("regex e1"); | |
stopwatch<double> time_e2("regex e2"); | |
stopwatch<double> time_find("string find"); | |
for (size_t cnt = 0; cnt < 10; ++cnt) | |
{ | |
time_e1.resume(); | |
for (size_t i = 0; i < 100000; ++i) | |
std::regex_match(str, sm, e1); | |
time_e1.pause(); | |
time_e2.resume(); | |
for (size_t i = 0; i < 100000; ++i) | |
std::regex_match(str, sm, e2); | |
time_e2.pause(); | |
time_find.resume(); | |
for (size_t i = 0; i < 100000; ++i) | |
{ | |
pos = str.find(':'); | |
key = str.substr(0, pos); | |
value = str.substr(pos + 1); | |
} | |
time_find.pause(); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Stopwatch - measure accurate time | |
// | |
// Mingjie Li ([email protected]) | |
// Mar 14, 2014 | |
// | |
// Compiled with | |
// - MinGW g++ 4.8.2 | |
// - Visual Studio Express 2013 | |
// | |
#include <chrono> // clock, duration, time_point | |
#include <ratio> // ratio | |
#include <string> // string | |
#include <iostream> // cerr | |
#include <iomanip> // fmt | |
// | |
// stopwatch - Measure execution time of function or any piece of code. | |
// | |
// Prerequisite - c++11 library <chrono> | |
// | |
// - Measure function execution time | |
// void function() | |
// { | |
// stopwatch<double> time("name"); // stopwatch starts | |
// [Some Code] | |
// } // stopwatch stops since out of scope. | |
// | |
// - Measure execution time of certain code | |
// // stopwatch declared and paused | |
// stopwatch<> *time = new stopwatch<>("name"); | |
// while (true) | |
// { | |
// time->start(); | |
// [Some Logic] | |
// time->pause(); | |
// [Console Output] | |
// time->resume(); | |
// [Other Logic] | |
// time->stop(); | |
// [Console Output] | |
// } | |
// delete time; // stopwatch stops by explicitly calling destructor. | |
// | |
template <typename T = int, typename R = std::milli> | |
class stopwatch | |
{ | |
private: | |
typedef std::chrono::high_resolution_clock hr_clock; | |
typedef std::chrono::high_resolution_clock::duration hr_duration; | |
typedef std::chrono::high_resolution_clock::time_point hr_time_point; | |
enum state_type | |
{ | |
state_start, | |
state_pause, | |
state_resume, | |
state_stop | |
}; | |
std::string _name; | |
bool _ticking; | |
hr_duration _duration; // sum(pause - resume) | |
hr_time_point _time_point; // Last time that starts ticking | |
public: | |
stopwatch(std::string name = "stopwatch") | |
{ | |
_name = name; | |
_ticking = false; | |
reset(); | |
} | |
virtual ~stopwatch() | |
{ | |
stop(); | |
} | |
void reset() | |
{ | |
if (_ticking) | |
{ | |
stop(); | |
} | |
_duration = hr_duration::zero(); | |
} | |
void start() | |
{ | |
reset(); | |
print(state_start); | |
_ticking = true; | |
_time_point = hr_clock::now(); | |
} | |
void pause() | |
{ | |
if (_ticking) | |
{ | |
_duration += hr_clock::now() - _time_point; | |
_ticking = false; | |
print(state_pause); | |
} | |
} | |
void resume() | |
{ | |
if (!_ticking) | |
{ | |
print(state_resume); | |
_ticking = true; | |
_time_point = hr_clock::now(); | |
} | |
} | |
void stop() | |
{ | |
if (_ticking) | |
{ | |
_duration += hr_clock::now() - _time_point; | |
_ticking = false; | |
print(state_stop); | |
} | |
} | |
private: | |
void print(state_type state) | |
{ | |
using namespace std; | |
using namespace std::chrono; | |
ios::fmtflags fmt(cerr.flags()); // keep cerr format | |
// Print stopwatch name, e.g. | |
// stopwatch [name ] | |
cerr << "[" << left << setw(16) << _name << right << "]"; | |
if (state == state_start) | |
{ | |
cerr << " starts..."; | |
} | |
else if (state == state_resume) | |
{ | |
cerr << " resume..."; | |
} | |
else | |
{ | |
unsigned long long min, sec, msec, usec; | |
// Default duration | |
usec = duration_cast<std::chrono::microseconds>(_duration).count(); | |
// Roundup usec to msec | |
usec += 500ULL; | |
// 0m00.000s | |
min = usec / 60000000ULL; | |
sec = (usec / 1000000ULL) % 60ULL; | |
msec = (usec / 1000ULL) % 1000ULL; | |
// Duration specified by template. | |
T ticks = duration_cast<duration<T, R>>(_duration).count(); | |
// Print execution time, e.g. | |
// elapsed 0m11.621s / 11620.665 ticks of 1/1000s | |
if (state == state_pause) | |
{ | |
cerr << " paused. "; | |
} | |
else if (state == state_stop) | |
{ | |
cerr << " done. "; | |
} | |
cerr << setw(3) << min << "m" | |
<< setfill('0') | |
<< setw(2) << sec << "." | |
<< setw(3) << msec << "s / " | |
<< setfill(' ') | |
<< setprecision(3) << fixed | |
<< setw(12) << ticks << " x " | |
<< R::num << '/' << R::den << "s"; | |
} | |
cerr << endl; | |
cerr.flags(fmt); // restore cerr format | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment