Is get or basename() more efficient?
which of the following is more efficient?
Using a get function on a token (random), for example:
http://www.example.com/category/subcategory/subsubcategory?value=random $_GET['value']
Make the token part of the URL and parse it, for exam开发者_Python百科ple:
$url="http://www.example.com/category/subcategory/subsubcategory/random" basename($url)
And actually, is basename more efficient than using explode or substr(strrchr())
?
$_GET is obviously more efficient, since it doesn't compute anything
Still, unless you plan on calling that a few thousand times in your script, it's negligible so use whatever you feel works better.
$t = microtime(true);
for($i = 0; $i<1000; $i++) {
$x = basename($url);
}
printf("%.3f\n", microtime(true) - $t);
0.010
Use basename
. There is no need to make another function that you already have.
Also in this case performance is completly insignificant
- Don't sweat micro-optimizations.
- If you want to sweat micro-optimizations, it's pretty trivial to test #1 vs #2 yourself.
- Regardless of minimal performance differences, #2 is generally preferred because you get a friendly URL.
精彩评论