Launch using
PHP 5.3.2 (cgi-fcgi) (built: Mar 3 2010 20:47:00) Copyright (c) 1997-2010 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies with Zend Debugger v5.3, Copyright (c) 1999-2010, by Zend Technologies
gives
dynamic path pathinfo 3.4931519031525 fnmatch 2.8633069992065 x0.82 static path pathinfo 0.83261299133301 fnmatch 0.28636598587036 x0.34
These four
function hasExtension1($ext, $filename) { return !strcasecmp(pathinfo($filename, PATHINFO_EXTENSION), $ext); } function hasExtension2($ext, $filename) { return fnmatch("*.$ext", $filename, FNM_CASEFOLD); } function hasExtension3($ext, $filename) { return strripos($filename, $ext) === strlen($filename) - strlen($ext); } function hasExtension4($ext, $filename) { return !strcasecmp(substr($filename, -strlen($ext)), $ext); }
at startup like
for($i=0;$i<10000;$i++) hasExtension1('php', __FILE__); for($i=0;$i<10000;$i++) hasExtension2('php', __FILE__); for($i=0;$i<10000;$i++) hasExtension3('php', __FILE__); for($i=0;$i<10000;$i++) hasExtension4('php', __FILE__);
and profiled on my machine from Zend Studio gives
Average Mean Time = 0.000007 Average Mean Time = 0.000006 Average Mean Time = 0.000005 Average Mean Time = 0.000003
It seems to me that this is somewhat annoying, that there is no 4 faster, but this is what he says. And with 0.00000n seconds per call, this is nothing to worry about anyway.
Gordon
source share