note 94512 added to function.microtime
| From: | shavais at gmail dot com | Date: | Mon, 09 Nov 2009 19:44:27 +0000 |
| Subject: | note 94512 added to function.microtime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162676@lists.php.net to get a copy of this message | ||
<?php
function endswith1($s, $e) {
return ( substr($s, strlen($s) - strlen($e)) == $e );
}
function endswith2($s, $e) {
$e = preg_quote($e);
return preg_match("/($e)$/", $s);
}
function control($s, $e) {return null;}
function timereps($func, $reps) {
$s = "path/to/file/somefile.csv";
$e = ".csv";
$t1 = microtime(true);
for ($i = 0; $i< $reps; $i++) {
$dmy = $func($s, $e);
}
$t2 = microtime(true);
return $t2 - $t1;
}
print "substr time: ".($e1 = timereps('endswith1', 370000))."\n";
print "preg_match time: ".($e2 = timereps('endswith2', 370000))."\n";
print "control time: ".($ct = timereps('control', 370000))."\n";
print "preg_match vs substr: ".(100.0 * (($e2-$ct) / ($e1-$ct)))."\n";
?>
When I run the above, I get widely varying results, but most of the results show the substr/strlen
version outperforming the preg_match version by between about 30 to 40% if separated out into a
dedicated function call, or 70 to 80% if done in-line (if I take out the time it takes to make a
function call):
substr time: 0.66379904747009
preg_match time: 0.91860914230347
control time: 0.32123684883118
preg_match vs substr: 174.38359978007
Both, however, on my (relatively high powered) office desktop, can fairly reliably be done 370,000
times or more in one second, so, the slower performance of preg_match may be a non-issue in the vast
majority of cases.
----
Server IP: 69.147.83.197
Probable Submitter: 173.11.31.153
----
Manual Page -- http://www.php.net/manual/en/function.microtime.php
Edit -- https://master.php.net/note/edit/94512
Del: integrated -- https://master.php.net/note/delete/94512/integrated
Del: useless -- https://master.php.net/note/delete/94512/useless
Del: bad code -- https://master.php.net/note/delete/94512/bad+code
Del: spam -- https://master.php.net/note/delete/94512/spam
Del: non-english -- https://master.php.net/note/delete/94512/non-english
Del: in docs -- https://master.php.net/note/delete/94512/in+docs
Del: other reasons-- https://master.php.net/note/delete/94512
Reject -- https://master.php.net/note/reject/94512
Search -- https://master.php.net/manage/user-notes.php