Regex vs. string:find() for simple word boundary

Posted by user576267 on Stack Overflow See other posts from Stack Overflow or by user576267
Published on 2011-01-16T16:48:10Z Indexed on 2011/01/16 16:53 UTC
Read the original article Hit count: 121

Say I only need to find out whether a line read from a file contains a word from a finite set of words.

One way of doing this is to use a regex like this:

.*\y(good|better|best)\y.*

Another way of accomplishing this is using a pseudo code like this:

 if ( (readLine.find("good")   != string::npos) ||
      (readLine.find("better") != string::npos) ||
      (readLine.find("best")   != string::npos) )
 {
   // line contains a word from a finite set of words.
 }

Which way will have better performance? (i.e. speed and CPU utilization)

© Stack Overflow or respective owner

Related posts about regex

Related posts about performance-comparison