开发者

Is get or basename() more efficient?

开发者 https://www.devze.com 2023-03-10 19:53 出处:网络
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

which of the following is more efficient?

  1. Using a get function on a token (random), for example:

    http://www.example.com/category/subcategory/subsubcategory?value=random $_GET['value']

  2. 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


  1. Don't sweat micro-optimizations.
  2. If you want to sweat micro-optimizations, it's pretty trivial to test #1 vs #2 yourself.
  3. Regardless of minimal performance differences, #2 is generally preferred because you get a friendly URL.
0

精彩评论

暂无评论...
验证码 换一张
取 消