dongyo1959
dongyo1959
2011-08-05 04:24
浏览 52
已采纳

编写一个PHP脚本,在垂直行中生成数字1到10

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?
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dqsh30374
    dqsh30374 2011-08-05 14:54
    已采纳

    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
    2
    3
    4
    5
    6
    7
    8
    9
    10</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+'
    ';}</script>";
    

    Here's a fiddle to see it in action.

    点赞 评论
  • dpp80346
    dpp80346 2011-08-05 06:12

    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/>";
    
    点赞 评论
  • donglu7998
    donglu7998 2011-08-05 06:26

    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.

    点赞 评论
  • doumao6212
    doumao6212 2011-08-05 16:04

    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.

    点赞 评论

相关推荐