Created
December 14, 2012 03:16
-
-
Save Lewuathe/4282421 to your computer and use it in GitHub Desktop.
regex.hとloopのパフォーマンス比較 ref: http://qiita.com/items/b749303f88dd030b339c
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
$ time ./regexTime | |
real 0m0.005s | |
user 0m0.002s | |
sys 0m0.002s |
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
$ time ./loopTime | |
real 0m0.005s | |
user 0m0.002s | |
sys 0m0.002s |
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
int i,j; | |
int c, l = strlen(str); | |
for (i = j = 0; i < l; i++) { | |
c = OPENSSL_VERSION_TEXT[i]; | |
if ('0' <= c && c <= '9') { | |
for (j = i + 1; j < l; j++) { | |
c = str[j]; | |
if (c == ' '){ | |
break; | |
} | |
} | |
break; | |
} | |
} | |
for( int k = i ; k < j - i + 2; k++ ){ | |
putchar(str[k]); | |
} | |
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
regex_t preg; | |
size_t nmatch = 5; | |
regmatch_t pmatch[nmatch]; | |
int ret = regcomp(&preg, "[[:digit:]]+.[[:digit:]].[[:digit:]]", | |
REG_EXTENDED | REG_NEWLINE); | |
if( ret ){ | |
cout << "regex compile failed" << endl; | |
} | |
cout << "String: " << str << endl; | |
if( regexec(&preg, str, nmatch, pmatch, 0) != 0 ){ | |
cout << "no match" << endl; | |
} | |
else{ | |
for( int i = 0 ; i < nmatch ; i++ ){ | |
for( int j = pmatch[i].rm_so ; j < pmatch[i].rm_eo ; j++ ){ | |
cout << str[j]; | |
} | |
} | |
cout << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment