19

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?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
JasonDavis
  • 48,204
  • 100
  • 318
  • 537

5 Answers5

26

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.

Community
  • 1
  • 1
Crozin
  • 43,890
  • 13
  • 88
  • 135
  • 1
    +1 interesting find, also interesting to see that this benchmark also (partly) seems to favour INIs over PHP includes. Google's translation quality is superb by the way, I initially thought I was reading a original english text. – Pekka Jan 22 '10 at 21:40
  • Odd that a serialized array would be faster than plain PHP. Surely more work would be involved in unserializing it. I think the relative performances should be commented on as well though, ini files are slower than plain PHP but by such a tiny margin in the benchmarks that it's pretty much negligible. That's interesting. – GordonM Feb 08 '12 at 10:46
  • 2
    @GordonM: Surely unserializing requires **less** work that interpreting PHP script. All you need is really simple parser that reads data and converts it into PHP data structures. On the other hand PHP interpreter has to do lots of work: parse a file using much more complex parser, compile source into bytecode, run the bytecode and return the results - much, much more work. – Crozin Feb 08 '12 at 11:25
13

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.

dennis
  • 620
  • 8
  • 21
  • 2
    I just want to point out (I know this is old) but this bypasses an important security aspect of INI files (they do not contain or execute code in the environment). By calling `include` you are allowing code to be (possibly) executed. Which is one of the reasons I favor `ini` files over php files. – Mike Dec 08 '15 at 22:27
7

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.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 2
    If you do, I'd be very interested in the results! – Pekka Jan 22 '10 at 21:02
  • Did they (also) test it using something like apc? On the other hand you might not want your "configuration" file to be able to execute code. – VolkerK Jan 22 '10 at 21:24
  • In php 5.4.13 I could confirm that the ini is 10% faster for two values and 30% faster for 20 values. (no sections) – mheinzerling Apr 16 '13 at 09:41
  • When you say parsing, you really mean read the file + compiling and parsing. If the php file uses opcode, parsing a pre-compiled array code will definitely be faster than reading an ini file *and* parse it. It you use a variable cache in memory for your ini string, it's likely to be close between those two. A performance comparison where both are stored in memory would be interesting. – hexalys Feb 08 '15 at 00:28
0

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

-4

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

streetparade
  • 32,000
  • 37
  • 101
  • 123