2

I recently gave an interview at a company where they asked me the following question:

"If server/site optimization is most important, please write a PHP script that generates the numbers 1 through 10 in a vertical line - IE - 1, then a line break, then 2, then a line break, etc."

While I could write this program in so many ways I don't think any of them is better than others in terms of server load and performance. For example:

for ($i = 1; $i <= 10; $i++) {
    echo $i . '<br />';
}

So I have got 2 questions here:

  1. Can we really optimize this code for performance?
  2. If yes, how? If not, how to respond to this question?
2
  • Codegolf.SE is for contests, as phrased this seems a better fit for Stack Overflow. I missed this earlier because of work pressure so I won't migrate it right away, but that is my intent if no one steps up with a strong defense of the appropriateness of CodeGolf.SE. Aug 11, 2011 at 22:08
  • Using echo seems to be necessary to strictly meet the "PHP script" requirement. Hard coding the echo with a static string would be a really dumb solution though as it doesn't optimize performance compared to just serving it as a static page. Another answer would be to use a reverse proxy - then the performance of the php would hardly matter whatsoever. I agree that this should be migrated to SO Aug 12, 2011 at 0:14

4 Answers 4

5

When trying to write efficient code, there are general practices that I try to follow right from the beginning of a project.

In the example you mention, I try to avoid making calls to echo repeatedly, as you can achieve significant performance enhancements by building up the code you want to present and then echoing it in one single call. For example:

for ($i = 1, $string = ''; $i <= 10; $i++) {
    s .= $i.'<br />';
}

echo $string;

Sure, in this example it makes little overall difference, but if you don't follow this type of simple convention from the beginning of a project, you'd have massive refactoring to do later on instead of just getting this right from the start.

2
  • 1
    Indeed, building up a string is much more efficient than echoing (I wasn't aware of that until now, thanks!). On the other hand, echoing to an output buffer is slightly more efficient than building a string (at least if you have a lot of text).
    – Joey Adams
    Aug 5, 2011 at 6:57
  • Amazing. I didn't know echoing is slower than building a string and then echoing (Really). Aug 6, 2011 at 7:13
5

Hopefully, this is a trick question. The correct response, in my opinion, would be to ask, "Is printing this little bit of text really our bottleneck?" Time spent making something faster is wasted time if it has a negligible impact on an application's overall speed.

On the other hand, Adam makes a really good point in his answer. Some bottlenecks are hard to fix after the coding is done.

Nevertheless, the following should be faster, mainly because it reduces interpreter overhead:

echo "1<br/>2<br/>3<br/>4<br/>5<br/>6<br/>7<br/>8<br/>9<br/>10<br/>";
1
  • 4
    or you pull the string outside the <?php ... ?> tags Aug 5, 2011 at 12:50
3

Because the server load is most important, I'd say the cheapest way is to deliver the minimum content to your users over the network with very little CPU load. Sending straight text will cause the lightest CPU load for similarly sized programs, so in this case, I'd go with a solution similar to Joey Adams's solution, though I'd remove 5 chars. Also, I've updated with Peter Taylor's suggestion of using the pre tag (48 chars - 40 over network):

echo "<pre>1\n2\n3\n4\n5\n6\n7\n8\n9\n10</pre>";

This is better in this case because the requirement is short. However, let's say we wanted to optimize 100 numbers and line breaks for the server. In that case, if you calculate it in PHP and send the full result over the internet, that would be 9 + 9*3 + 90*4 + 3 = 399 characters plus additional server overhead for the calculations if you did not hard-code it. Thus, instead of performing the logic within PHP and then sending the full result to the client, you could send a smaller script to the client that will build the content for you while at the same time reducing the load on your server. Here's an example bit of JavaScript/HTML you could build and send with PHP (136 chars - 128 over network):

echo "<pre id=\"c\"><\pre><script type=\"text/javascript\">for(i=0;i++<100;){document.getElementById('c').innerHTML+=i+'\n';}</script>";

Here's a fiddle to see it in action.

4
  • You could use a <pre> tag and newlines to reduce the amount of data sent over the network even more in the first case. In the second case, <div id="c"></div> is more robust than <div id="c"/>, which some browsers don't like. Aug 5, 2011 at 15:58
  • @Peter Taylor: Thanks much, I've updated my answer based on your suggestions.
    – Briguy37
    Aug 5, 2011 at 16:36
  • But, what if JavaScript is disabled
    – Mob
    Aug 6, 2011 at 14:51
  • @Mobinga: One way of handling users where JavaScript is disabled is to initially set the body of the <pre> tag to "Please enable JavaScript to view this content." Another way is to conditionally provide static content if a client has JavaScript disabled. Personally, I'd start with the first option to get the product out there and iron out major bugs, then implement a counter to determine the percentage of clients with JavaScript disabled, and finally move to the second solution if the percentage is high enough.
    – Briguy37
    Aug 8, 2011 at 14:08
2

Your interviewer wanted the following:

1
2
3
4
5
6
7
8
9
10

That is the fastest way to do it, hands down.

4
  • How is that the fastest? Where's the PHP?
    – Mob
    Aug 6, 2011 at 0:03
  • 1
    Re: "Where's the PHP?" -- that's exactly the point. If you don't put in a PHP tag, the interpreter just flies on by.
    – boothby
    Aug 6, 2011 at 1:56
  • @boothby Guess you didn't read the question properly. It is to write a PHP script that does this. What I interpret from this is to at least use PHP's start and end tags. Aug 6, 2011 at 7:11
  • 1
    This is a valid PHP script, I don't understand your problem. You were asked to reduce server load as much as possible. This does that. If I was performing the interview, and I didn't want to see this answer, at the very least, I'd give the interviewee credit for thinking outside the box and clarify the question. If I was looking for this answer and the interviewee failed to give it, they'd get a blank look and no callback.
    – boothby
    Aug 6, 2011 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.