dongpan1365 2013-08-29 17:46
浏览 71
已采纳

White Spaces,Comments,Empty Lines会增加PHP的执行时间吗? [重复]

This question already has an answer here:

i have this doubt from many days. in every PHP code i write, i will write many comments, leave many white spaces and leave 4 to 5 empty lines between section and section (to make it clear for me)

will all these empty spaces, comments, empty lines make my PHP code to run slow ?

personal experiences are much appreciated :)

</div>
  • 写回答

4条回答 默认 最新

  • dpevsxjn809817 2013-08-29 18:09
    关注

    This is really a matter of IO and hard drive speed. If your bare file is 10KB and comments and line breaks add 4KB then the extra time that the hard drive spends reading more KB is what you need to benchmark (it's negligible by the way), not even worth your time.

    If you start getting into micro-optimization then you run the risk of making your code absolutely horrid to read and maintain.

    The best way to speed up your code is to re-factor code where necessary and don't do silly things that obviously hog resources like this crude example:

    <?php
    
    $arr = array(); // pretend it has 50,000 items
    
    //GOOD IDEA: count the array once and reference that number
    $arr_count = count($arr);
    for($i=0; $i < $arr_count; $i++){
        echo $arr[$i];
    }
    
    
    //BAD IDEA: re-counting the array for every iteration
    for($i=0; $i < count($arr); $i++){
        echo $arr[$i];
    }
    
    ?>
    

    Also unsetting a large array after you are done using it is better than waiting for the Garbage Collector to kick in. For example: pulling data from DB and looping through it. Unset the data when done and keep coding.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突