Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strstr vs regex in c

Tags:

c

regex

gcc

strstr

Let's say, for example, I have a list of user id's, access times, program names, and version numbers as a list of CSV strings, like this:

1,1342995305,Some Program,0.98
1,1342995315,Some Program,1.20
2,1342985305,Another Program,15.8.3
1,1342995443,Bob's favorite game,0.98
3,1238543846,Something else,
...

Assume this list is not a file, but is an in-memory list of strings.

Now let's say I want to find out how often a program has been accessed to certain programs, as listed by their version number. (e.g. "Some Program version 1.20" was accessed 193 times, "Some Program version 0.98" was accessed 876 times, and "Some Program 1.0.1" was accessed 1,932 times)

Would it be better to build a regular expression and then use regexec() to find the matches and pull out the version numbers, or strstr() to match the program name plus comma, and then just read the following part of the string as the version number? If it makes a difference, assume I am using GCC on Linux.

Is there a performance difference? Is one method "better" or "more proper" than the other? Does it matter at all?

like image 250
cegfault Avatar asked Jun 20 '26 08:06

cegfault


1 Answers

Go with strstr() - using regex to count a number of occurrences is not a good idea, as you would need to use loop anyway, so I would suggest you to do a simple loop with searching for poistion of substring and increase counter and starting search position after each match.

like image 113
Ωmega Avatar answered Jun 22 '26 23:06

Ωmega



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!