Last active
June 12, 2016 03:36
-
-
Save usagi/792b3ca54842279616b0e689e740c410 to your computer and use it in GitHub Desktop.
C++ で `std::string in` に対する `in == "http" or in "https"` 的なパターンとコストの比較 ref: http://qiita.com/usagi/items/718f573e27dd599c32e6
This file contains hidden or 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
in == "http" or in == "https" |
This file contains hidden or 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
auto f = | |
[] ( auto g ) | |
{ auto t0 = std::chrono::steady_clock::now(); | |
g(); | |
std::cout << ( std::chrono::steady_clock::now() - t0 ).count() << ' '; | |
}; |
This file contains hidden or 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
// for reference | |
f( [ ]{ return true; } ); | |
// for reference | |
f( [&]{ return in == "http"; } ); | |
// 1. simple eq-or pattern | |
f( [&]{ return in == "http" or in =="https"; } ); | |
// 2. substr-eq pattern | |
f( [&]{ return in.substr( 0, 4 ) == "http"; } ); | |
// 3. size-strcmp pattern | |
f( [&]{ return in.size() > 3 and std::strcmp( "http", in.c_str() ); } ); | |
// 4. find pattern | |
f( [&]{ return in.find( "http" ) == 0; } ); | |
// 5. boost::xpressive pattern | |
f( [&]{ using namespace boost::xpressive; smatch r; sregex p = bos >> as_xpr( "http" ) >> ( ! as_xpr( 's' ) ) >> eos; return regex_match( in, r, p ); } ); |
This file contains hidden or 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 <chrono> | |
#include <boost/xpressive/xpressive.hpp> | |
auto main() -> int | |
{ | |
auto f = | |
[] ( auto g ) | |
{ auto t0 = std::chrono::steady_clock::now(); | |
g(); | |
std::cout << ( std::chrono::steady_clock::now() - t0 ).count() << ' '; | |
}; | |
using namespace std::literals::string_literals; | |
for ( const auto& in : { "https"s, "https@https@https@https@https@https@https@https@https@https@https@https@https@https@https@https@"s } ) | |
{ | |
for ( auto n = 0; n < 8; ++n ) | |
{ | |
// for reference | |
f( [ ]{ return true; } ); | |
// for reference | |
f( [&]{ return in == "http"; } ); | |
// 1. simple eq-or pattern | |
f( [&]{ return in == "http" or in =="https"; } ); | |
// 2. substr-eq pattern | |
f( [&]{ return in.substr( 0, 4 ) == "http"; } ); | |
// 3. size-strcmp pattern | |
f( [&]{ return in.size() > 3 and std::strcmp( "http", in.c_str() ); } ); | |
// 4. find pattern | |
f( [&]{ return in.find( "http" ) == 0; } ); | |
// 5. boost::xpressive pattern | |
f( [&]{ using namespace boost::xpressive; smatch r; sregex p = bos >> as_xpr( "http" ) >> ( ! as_xpr( 's' ) ) >> eos; return regex_match( in, r, p ); } ); | |
std::cout << '\n'; | |
} | |
std::cout << '\n'; | |
} | |
} |
This file contains hidden or 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
593 5884 3070 507 190 5990 71745 | |
190 365 268 277 190 260 7513 | |
177 220 262 258 190 228 5702 | |
178 220 262 250 190 227 5447 | |
178 220 262 250 190 227 5318 | |
175 223 263 257 190 227 5275 | |
175 220 263 257 190 228 5272 | |
175 220 262 258 190 227 5300 | |
178 220 262 273 187 228 5325 | |
178 220 262 250 190 228 5272 | |
175 220 263 250 190 228 5237 | |
175 220 262 250 190 228 5245 | |
175 220 262 250 190 228 5252 | |
177 220 265 253 187 227 5238 | |
175 220 263 250 190 227 5240 | |
178 220 263 252 190 228 5282 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment