friedemann_bach Posted August 13, 2008 Share Posted August 13, 2008 Dear Forum, which of the following lines is the more efficient one, regarding parsing time? Or are they all the same? (1) $string = $a.' > '.$b.' > '.$c.' > '.$d; (2) $string = "$a > $b > $c > $d"; I've done some benchmark tests to find out that the second one is only slightly more efficient - can you confirm that? In which cases would you prefer the first method? Quote Link to comment https://forums.phpfreaks.com/topic/119451-code-optimization-issue/ Share on other sites More sharing options...
discomatt Posted August 13, 2008 Share Posted August 13, 2008 In theory, method A is slightly faster. Results will vary computer to computer, but the difference in parsing time is insignificant. It comes down to preference. Quote Link to comment https://forums.phpfreaks.com/topic/119451-code-optimization-issue/#findComment-615424 Share on other sites More sharing options...
JonnoTheDev Posted August 13, 2008 Share Posted August 13, 2008 Why is speed an issue with such a code example? There are loads of methods for constructing a string as above, implode(), sprintf(), printf() are some functions that could do it? Are you running PHP on a 286 pc? Quote Link to comment https://forums.phpfreaks.com/topic/119451-code-optimization-issue/#findComment-615434 Share on other sites More sharing options...
discomatt Posted August 13, 2008 Share Posted August 13, 2008 Why is speed an issue with such a code example? There are loads of methods for constructing a string as above, implode(), sprintf(), printf() are some functions that could do it? Are you running PHP on a 286 pc? In large scale, high traffic scripts, milliseconds make a huge difference. Nothing wrong with attempting to make a script as efficient as possible. Quote Link to comment https://forums.phpfreaks.com/topic/119451-code-optimization-issue/#findComment-615442 Share on other sites More sharing options...
friedemann_bach Posted August 13, 2008 Author Share Posted August 13, 2008 This was just a simplified example. I have some frequently used scripts that handle a lot of string operations (no need to submit all the code here) and I want to optimize performance - every millisecond helps. Quote Link to comment https://forums.phpfreaks.com/topic/119451-code-optimization-issue/#findComment-615457 Share on other sites More sharing options...
JonnoTheDev Posted August 13, 2008 Share Posted August 13, 2008 You are better installing optimizers onto your web server such as the Zend PHP optimizer and if your string data is coming from a large database then using memcache may be an option Quote Link to comment https://forums.phpfreaks.com/topic/119451-code-optimization-issue/#findComment-615461 Share on other sites More sharing options...
friedemann_bach Posted August 13, 2008 Author Share Posted August 13, 2008 Ok. Thanks for your proposals. I don't have any experience with Zend and I can't say whether it would help in this issue -- however, I will give it a try. The thing is that my script can produce a different result every time, as it depends a lot on user input. It is an evaluation script that works for a number of different forms. Then, the script is meant to work on environments where developers do not always have the option to install optimizers on their servers on their own. Quote Link to comment https://forums.phpfreaks.com/topic/119451-code-optimization-issue/#findComment-615545 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.