0
std::wregex EXCEL_CELL_REGEX(L"=\"(.*)\"", std::regex::optimize);
std::wstring text = L"=\"300498\"";
for (int i = 0; i < 981 * 6; i++) {
    std::wsmatch match;
    std::regex_match(text, match, EXCEL_CELL_REGEX);
}

Above code takes about 9 seconds

boost::wregex EXCEL_CELL_REGEX(L"=\"(.*)\"", boost::regex::optimize);
std::wstring text = L"=\"300498\"";
for (int i = 0; i < 981 * 6; i++) {
    boost::wsmatch match;
    boost::regex_match(text, match, EXCEL_CELL_REGEX);
}

Above code takes about 1.5 seconds


Those tests are built on Debug configuration.

Do you know why std::regex is so slow? How to optimize the code?

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
user1633272
  • 2,007
  • 5
  • 25
  • 48
  • 8
    Use Release build for testing performance. – Jarod42 Jul 24 '18 at 16:34
  • Debug build may add consistency check. – Jarod42 Jul 24 '18 at 16:35
  • 7
    *Never* benchmark performance with debug builds. The code is not optimized by the compiler. There will be debug assertions (and more) active that are compiled out in release builds. Nobody actually cares about the performance of debug builds since that's never what you ship. *Always* benchmark optimized release builds. – Jesper Juhl Jul 24 '18 at 16:57
  • You are right, after switching to Release build, it tasks less than 1 second now. – user1633272 Jul 24 '18 at 17:28

2 Answers2

3

Debug execution times are useless; they are often completely divorced from real-life performance of the Release build. Also, debug times will be extremely system- and compiler-dependent.

PlinyTheElder
  • 1,454
  • 1
  • 10
  • 15
1

If you are using boost in debug configurations, you will got a lot of debug stuff who will probably don't need it. If you wish run in debug mode. Try look for memory and CPU snapshots to see where the bottleneck resides! I suggest you use the c++11 clock std lib functions to measure time elapsed with more precision.