PHP parse_ini_file() performance?

陌路散爱 提交于 2019-11-28 18:49:39
Crozin

According to this old blog post retrieved from web archives.

From fastest to slowest:

  1. Serialized arrays
  2. Plain PHP code
  3. INI files
  4. XML files
  5. YAML files

EDIT (08/02/2012)

If APC or other accelerator is available on the server plain PHP files would be the fastest due to fact that they will be parsed only once and kept in memory for further use.

For the other part of the question: If opcode cache caches ini files. At the time of writing, with my version PHP 5.3.5, the APC cache does not do it per automatic, but you can make sure that ini files are cached by APC by adding a function like the following and use that for parsing ini files:

function parse_ini_file_ext ($file, $sections = null) {
    ob_start();
    include $file;
    $str = ob_get_contents();
    ob_end_clean();
    return parse_ini_string($str, $sections);
}

Then ini files are cached by APC.Measured with a simple microtime benchmark this is also faster than reading the ini files directly.

I had always harboured the suspicion that parse_ini_file is dismally slow, and that storing variables in arrays in PHP files is faster. But there's this 2004 article that says otherwise:

And lastly we test storing configuration parameters in an INI file or in a PHP file as an associative array. We found that storing in an INI file and using parse_ini_file() is faster than parsing a PHP file.

I won't entirely believe this until I test it myself when I get around to it some time. But the article (and the magazine) look solid enough to be taken seriously.

The parse_ini_file built-in function is implemented in C. This makes it quite fast.

You will be much faster if you dont cache the ini file. All experts can cay that this is true.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!