Chinaunix

标题: 测试php代码,越在前面运行的越耗时 [打印本页]

作者: yakczh_cu    时间: 2014-06-16 12:07
标题: 测试php代码,越在前面运行的越耗时
本帖最后由 yakczh_cu 于 2014-06-16 12:32 编辑

test1.php



  1. <?php
  2. function test(){
  3. $i=100;
  4. $arr=array();
  5. do { array_push($arr,$i); }while ($i--);
  6. }


  7. $func='test';

  8. define('REPEAT',10000*10);
  9. $cnt=REPEAT;
  10. $start=microtime(true);
  11. do { $func(); } while ($cnt--);
  12. $end=microtime(true);
  13. echo "\ncall_by_name exec time " ,$end-$start;

  14. $cnt=REPEAT;
  15. $start=microtime(true);
  16. do { call_user_func($func); }while ($cnt--);
  17. $end=microtime(true);
  18. echo "\ncall_user_func exec time " ,$end-$start;
  19. ?>
复制代码
test2.php

  1. <?php
  2. function test(){
  3. $i=100;
  4. $arr=array();
  5. do { array_push($arr,$i); }while ($i--);
  6. }


  7. $func='test';

  8. define('REPEAT',10000*10);
  9. $cnt=REPEAT;
  10. $start=microtime(true);
  11. do { call_user_func($func); }while ($cnt--);
  12. $end=microtime(true);
  13. echo "\ncall_user_func exec time " ,$end-$start;

  14. $cnt=REPEAT;
  15. $start=microtime(true);
  16. do { $func(); } while ($cnt--);
  17. $end=microtime(true);
  18. echo "\ncall_by_name exec time " ,$end-$start;



复制代码

作者: yakczh_cu    时间: 2014-06-16 12:14
附运行结果

作者: action08    时间: 2014-08-12 07:48
22s, 21s,时间差不多呀




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2