Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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

Lewuathe
Java/Scala/Python/Hadoop/Presto
http://www.lewuathe.com
treasuredata
Customer Data Platformの開発・提供をしています。
https://www.treasuredata.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away