LoginSignup
0
0

More than 5 years have passed since last update.

regex.hとloopのパフォーマンス比較

Posted at

Using regex.h

regexTime.cc
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;
}

Using only for loop

loopTime.cc
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;
$ time ./regexTime
real    0m0.005s
user    0m0.002s
sys 0m0.002s
$ time ./loopTime
real    0m0.005s
user    0m0.002s
sys 0m0.002s

No difference

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0