PHP parse_ini_file() performance?

孤街浪徒 提交于 2019-12-29 03:28:07

问题


I know some people store settings in an .ini file and get the values with parse_ini_file() in PHP. Without running tests, I am curious about performance.

Do you know if opcode cache can cache any of this type of stuff if setting are in an ini file?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/2120401/php-parse-ini-file-performance

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