开发者

Write a PHP script that generates the numbers 1 through 10 in a vertical line

开发者 https://www.devze.com 2023-03-26 05:51 出处:网络
I recently gave an interview at a company where they asked me the fo开发者_C百科llowing question:

I recently gave an interview at a company where they asked me the fo开发者_C百科llowing 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?


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.


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/>";


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.


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.

0

精彩评论

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