I have a generic database query function that performs the following checks every time an SQL query is issued:
if (preg_match('~^(?:UPDATE|DELETE)~i', $query) === 1)if (preg_match('~^(?:UPDATE|DELETE)~iS', $query) === 1)if ((stripos($query, 'UPDATE') === 0) || (stripos($query, 'DELETE') === 0))
I know that a simple call to strpos() is faster than executing preg_match() , however, since I call strIpos() twice , I'm really not sure if you need to work better.
The template S modifier in the second option also causes some confusion in my head, from the manual:
When the template will be used several times, it is worth spending more time analyzing it to speed up the time spent on matching. If this modifier is installed, then additional analysis is performed. At present, examining a pattern is only useful for non-fixed patterns that make it not have a single fixed start character.
In this case, the speed is not critical (otherwise I would not use this general query function), but I would still like to make it as fast as possible, while preserving its simplicity.
Which of the above options should I choose?
EDIT: I am running a simple test , and yet I cannot decide which method works best.
Below are the results for 10,000 attempts (total time in seconds):
Array ( [match] => Array ( [stripos] => 0.0965 [preg_match] => 0.2445 [preg_match?] => 0.1227 [preg_match?S] => 0.0863 ) [no-match] => Array ( [stripos] => 0.1165 [preg_match] => 0.0812 [preg_match?] => 0.0809 [preg_match?S] => 0.0829 ) )
100,000 attempts :
Array ( [match] => Array ( [stripos] => 1.2049 [preg_match] => 1.5079 [preg_match?] => 1.5564 [preg_match?S] => 1.5857 ) [no-match] => Array ( [stripos] => 1.4833 [preg_match] => 0.8853 [preg_match?] => 0.8645 [preg_match?S] => 0.8986 ) )
1,000,000 attempts :
Array ( [match] => Array ( [stripos] => 9.4555 [preg_match] => 8.7634 [preg_match?] => 9.0834 [preg_match?S] => 9.1629 ) [no-match] => Array ( [stripos] => 13.4344 [preg_match] => 9.6041 [preg_match?] => 10.5849 [preg_match?S] => 8.8814 ) )
10,000,000 attempts :
Array ( [match] => Array ( [stripos] => 86.3218 [preg_match] => 93.6755 [preg_match?] => 92.0910 [preg_match?S] => 105.4128 ) [no-match] => Array ( [stripos] => 150.9792 [preg_match] => 111.2088 [preg_match?] => 100.7903 [preg_match?S] => 88.1984 ) )
As you can see, the results change a lot, this makes me wonder if this is the right way to run the test.