duanao3204 2013-10-24 13:40
浏览 48
已采纳

常数加载基准

I want to measure the exact loading time of a bunch of constants in php files. I've chosen 4 methods:

  • using a key-value pair array (return an array of data in php file)
  • using a class with constants in it
  • define
  • json (write json_encoded values into a file and the load them using a file_get_contents and json_decode)

First I created 4 different files with these methods and then filled them with 5000 random name and value pairs (same values for all).

I tried to load them in PHP and used microtime function to measure the time of loading but the results seems a bit strange! This is my test.php file:

// array
$time['array']['start'] = microtime(true);
$config_a = include('conf_array.php');
$time['array']['end'] = microtime(true);
// class
$time['class']['start'] = microtime(true);
include('conf_class.php');
$config_c = new Config();
$time['class']['end'] = microtime(true);
// define
$time['defin']['start'] = microtime(true);
include('conf_define.php');
$time['defin']['end'] = microtime(true);
// json
$time['json']['start'] = microtime(true);
$config_j = json_decode(file_get_contents('conf_json.json'));
$time['json']['end'] = microtime(true);

foreach ($time as $name => $item) {
    echo $name . ": " . (($item['end'] - $item['start']) * 1000) . " units.";
}

When I load the test.php with a set of new generated files, I'll get these results:

Array:  7.9629421234131 units.
Class:  6.5279006958008 units.
Defin:  19.877910614014 units.
Json:   4.4741630554199 units.

but when I hit refresh button (F5), the results will change! Here are the results of the same sample after refreshing the page:

Array:  1.7659664154053 units.
Class:  2.467155456543  units.
Defin:  6.4060688018799 units.
Json:   4.9409866333008 units.

And then, the order wont change anymore. It seems that php files (all except json) will load faster when you load them more than once. The same thing will happen if I restart apache (along with PHP).

I was wondering why is this happening?! Does it have something to do with some kind of caching?

  • 写回答

1条回答 默认 最新

  • dongluxin6711 2013-10-24 15:19
    关注

    I don't know if Apache itself does basic caching, but I think it shouldn't, by design. There could be a couple of modules installed that cause this behavior:

    1. mod_cache
    2. mod_file_cache
    3. APC which caches compiled PHP in memory. (and could explain why the JSON txt file is not affected)

    Maybe you have any of these, or maybe others that are the cause of this effect.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用