Created
July 5, 2014 20:12
-
-
Save mjrider/3ead6c77c75cb4d01b9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
optimalisation for path modification | |
dirname returns an \ on dirname('/') under windows | |
case 1: 'the right way' check if this is the case, and if so, fixit | |
case 1: 'the ugly way' just fixit | |
it is normal for this code path to be hit +1milion times per request | |
*/ | |
$max = 1000000; | |
$starttime1 = microtime(true); | |
$path = '/test/stuffs/'; | |
for ($i =0; $i<$max; $i++) { | |
if ($path[0] === '\\') { | |
$path[0] = '/'; | |
} | |
} | |
$endtime1 = microtime(true); | |
echo 'the right way:'.($endtime1 - $starttime1)."<br>\n"; | |
echo "<br>\n"; | |
$starttime2 = microtime(true); | |
for ($i =0; $i<$max; $i++) { | |
$path[0] = '/'; | |
} | |
$endtime2 = microtime(true); | |
echo 'the ugly way:'.($endtime2 - $starttime2)."<br>\n"; | |
echo "<br>\n"; | |
/* | |
next case adding an / after dirname() stripped it away | |
option 1: test if path[1] is set, ifso add a slash | |
option 2: test if the strlen is longer then 1 | |
*/ | |
$max = 1000000; | |
$starttime1 = microtime(true); | |
$path = '/test/stuffs/'; | |
for ($i =0; $i<$max; $i++) { | |
$testpath = dirname($path); | |
if (isset($testpath[1])){ | |
$testpath .= '/'; | |
} | |
} | |
$endtime1 = microtime(true); | |
echo 'isset:'.($endtime1 - $starttime1)."<br>\n"; | |
echo "<br>\n"; | |
$starttime2 = microtime(true); | |
for ($i =0; $i<$max; $i++) { | |
$testpath = dirname($path); | |
if (strlen($testpath) >= 2){ | |
$testpath .= '/'; | |
} | |
} | |
$endtime2 = microtime(true); | |
echo 'strlen:'.($endtime2 - $starttime2)."<br>\n"; | |
echo "<br>\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment