Test platform (virtual machine)
System: ubuntu 15.1
Compiler Version:
g++ --version
g++ 5.2.1
boost Version: boost 1.59
1. Regular Expressions regex
A regular expression used to match the search string.
Here is the test code.
c ++ 11 version test_regex.cpp
#include <stdio.h> #include <regex> #include <string> #include <time.h> bool matchIp(const std::string &message) { try { std::regex m_p("^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$"); return std::regex_match(message, m_p); } catch (std::exception &ex) { printf("std::regex err: %s", ex.what()); return false; } } int main () { setbuf(stdout, NULL); std::string ip = "192.168.0.128"; std::string ip2 = "192.aaa168.0.128"; clock_t start,end; start=clock(); for(int i = 0; i < 1000; i++) { bool match = matchIp(ip); bool match2 = matchIp(ip2); //printf("match:%d, match2:%d\n", (int)match, (int)match2); } end=clock(); printf("Thetimewas:%f\n",(double)(end-start)/CLOCKS_PER_SEC); return 0; }
Compile command g ++ test_regex.cpp -o test_regex -std = c ++ 11 -O2
boost version
#include <stdio.h> #include <boost/regex.hpp> #include <string> #include <time.h> bool matchIp(const std::string &message) { try { boost::regex m_p("^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$"); return boost::regex_match(message, m_p); } catch (std::exception &ex) { printf("boost::regex err: %s", ex.what()); return false; } } int main () { setbuf(stdout, NULL); std::string ip = "192.168.0.128"; std::string ip2 = "192.aaa168.0.128"; clock_t start,end; start=clock(); for(int i = 0; i < 1000; i++) { bool match = matchIp(ip); bool match2 = matchIp(ip2); //printf("match:%d, match2:%d\n", (int)match, (int)match2); } end=clock(); printf("Thetimewas:%f\n",(double)(end-start)/CLOCKS_PER_SEC); return 0; }
Compile command g ++ test_boost_regex.cpp -g -o test_boost_regex -I / boost_1_59_0 / /boost_1_59_0/stage/lib/libboost_regex.a /boost_1_59_0/stage/lib/libboost_system.a -O2
It matches the same IP, 1000 cycles, the time comparison
boost regular c ++ 11 standard library than the regular 40 times faster!
2. Thread thread test
c ++ 11 version
#include <stdio.h> #include <thread> #include <stdexcept> void func5(); void func2(); void func3(int &a); void func4(); void func5(); void func1() { func2(); } void func2() { int a = 2; func3(a); } void func3(int &a) { func4 (); } void func4() { Func5 (); } void func5() { throw std::runtime_error("test exception"); } int main () { std::thread t(&func1); t.join(); return 0; }
Compile command: g ++ test_thread.cpp -o test_thread -O2 -std = c ++ 11 -lpthread -g -lrt
Directly after running a leave, which we expected.
Check run with gdb
gdb ./test_thread
Our code exception thrown location information, call stack is completely gone.
boost version
#include <stdio.h> #include <boost/thread.hpp> #include <stdexcept> void func5(); void func2(); void func3(int &a); void func4(); void func5(); void func1() { func2(); } void func2() { int a = 2; func3(a); } void func3(int &a) { func4 (); } void func4() { Func5 (); } void func5() { throw std::runtime_error("test exception"); } int main () { boost::thread t(&func1); t.join(); return 0; }
Compile command: g ++ test_boost_thread.cpp -g -o test_boost_thread -I / boost_1_59_0 /boost_1_59_0/stage/lib/libboost_thread.a /boost_1_59_0/stage/lib/libboost_system.a -lpthread -lrt -O2
Similarly execution test_boost_thread quit unexpectedly.
Debugging with gdb
gdb ./test_boost_thread
You can clearly see
#7 0x0804a9dd in func5 () at test_boost_thread.cpp:32
#8 0x0804aa38 in func1() () at test_boost_thread.cpp:28
It places in the code where the exception is thrown, a very convenient location problem.
Thread section concludes that (this version) c ++ 11 standard library thread thread will swallow the exception stack, if a problem want to troubleshoot the cause, will be very difficult.