Skip to content

Instantly share code, notes, and snippets.

@Lewuathe
Created December 14, 2012 03:16
Show Gist options
  • Save Lewuathe/4282421 to your computer and use it in GitHub Desktop.
Save Lewuathe/4282421 to your computer and use it in GitHub Desktop.
regex.hとloopのパフォーマンス比較 ref: http://qiita.com/items/b749303f88dd030b339c
$ time ./regexTime
real 0m0.005s
user 0m0.002s
sys 0m0.002s
$ time ./loopTime
real 0m0.005s
user 0m0.002s
sys 0m0.002s
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;
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