-3

I need to run a check on multiple variables, some of which will be urls and others will only contain no more than 5 or 6 characters, none of which will ever be "http".

Which of the two methods below gives the best performance in terms of speed and processor load.

$str = 'http://somereallylongwebaddress.com';

if (substr( $str, 0, 4 ) === "http") {
    // true
}

if (strlen( $str >= 7 )) {
    // true
}

EDIT For anybody else that is interested I found this great page which runs live comparisons of various different functions. It doesn't address my particular one but very informative all the same.

spice
  • 1,442
  • 19
  • 35
  • You know that the two statements test entirely different things, right? I suspect `strlen()` is faster, but it should be easy to test. Have you benchmarked this yourself? – ChrisGPT was on strike Mar 29 '18 at 00:54
  • Yes I know what they both do, just asking which would be more efficient in this use case. No I haven't benchmarked it, not really sure how to do that which is why I'm asking. – spice Mar 29 '18 at 00:55
  • 3
    You write code that uses both methods, each in a loop, and time the performance of each in order to compare them. – Ken White Mar 29 '18 at 00:56
  • 2
    What Ken White said. But the performance difference is probably negligible when compared with the rest of your program. "Premature optimization is the root of all evil." – ChrisGPT was on strike Mar 29 '18 at 00:57
  • So just set a finite loop and time it? Ahh ok, thought there was more to it than that. Will give it a try. – spice Mar 29 '18 at 00:57
  • @Chris I totally agree, but in this instance this routine will be running on thousands of queries every few minutes. Just wanted to make sure I was taking the best approach from the outset. – spice Mar 29 '18 at 00:59
  • 3
    The answer here, really, is "it really doesn't matter". You've spent more time worrying about this than your server will save in twenty years of operation. I just tested it - **10 MILLION** executions of these two bits of code take **0.6 and 0.4 seconds**, respectively (and that's on my rather slow laptop). Hence, "premature optimization". – ceejayoz Mar 29 '18 at 01:09
  • @ceejayoz haha fair enough, point taken and thanks for the running the test and humouring me ;) – spice Mar 29 '18 at 01:13

2 Answers2

1

You can time performance of any code in PHP by doing the following:

$msc = microtime(true);
// YOUR CODE
$msc = microtime(true)-$msc;
echo $msc;
Gregory R.
  • 1,815
  • 1
  • 20
  • 32
1

You can run following code in any of online php editors below using different inputs and observe the speed performance.

http://www.writephponline.com/

https://www.tutorialspoint.com/php_webview_online.php

<?php
$before = microtime(true);
$str = "http";
if (substr( $str, 0, 4 ) === "http") {
    // true
}
echo "strlen performance ";
echo "Time:  " . number_format(( microtime(true) - $before), 8) . " Seconds\n";

echo "\r\n";

$before = microtime(true);
if (strlen($str >= 4)) {
    // true
}
echo "substr performance ";
echo "Time:  " . number_format(( microtime(true) - $before), 8) . " Seconds\n";
echo "\r\n";
?>

Based on multiple results from above code snippet, substr is showing better performance in terms of speed. In terms of processor load, assembly code for each function needs to compare.

Swati
  • 577
  • 6
  • 15
  • 1
    Running each command once tells you absolutely nothing. At the very least you should run the comparison many times in a loop. Ideally you should also test a variety of inputs, both matching and not-matching. – ChrisGPT was on strike Mar 29 '18 at 12:24