📜 ⬆️ ⬇️

The fastest settings for PHP scripts

Probably, everyone who has come up with the development of more or less serious applications knows that the choice of the storage format of the script or application settings is a fairly important matter. Configs should be easy to read, easily modified, easily portable, and so on - the list goes on and on.

Since server-side PHP scripts are executed, it happens many times per second, the download speed of configs is quite an important parameter. Although he, at times, is not paid much attention. Let's compare the various options for storing settings for PHP scripts in terms of their speed. Well, let us touch briefly on their convenience.

So, the experimental:
To offend no one, the listing is in alphabetical order. The option of storing the settings in the database, by the way, was not considered. Too unprofitable it looks in terms of speed of access to the settings.

Conditions:
It is clear that one can argue with the second of the conditions of testing. I found this option optimal for storing settings in memory while the script is running, but in some cases it is not. And, by the way, this condition is not satisfied with PHP scripts with definitions, which is why they were marked “out of competition”.
')
I do not cite the configuration of the equipment. It is clear that the speed of scripts depends on the server, but in this case the scripts are compared, and not the servers.

True, it is necessary to make a small clarification about the server software. A real web server was used, at the time of low load. Accordingly, the configuration of the server is “combat”: Linux Debian Lenny, a lot of memory and a RAID1 array of hard drives. PHP series 5.2.x (not the latest, though) with eAccelerator. At the time of the tests, Zend Optimizer turned off so that the tests were more “pure”, which minimally affected the results. Tests without eAccelerator were also conducted, but, oddly enough, this did not affect the distribution of forces much. The reason, in my opinion, lies in the fact that eAccelerator is configured for disk caching of PHP opcodes and for comparing file modification times, which “eats” a certain amount of time - although it brings certain bonuses.

Ini files


: 0.015, 0.086, 0.784

:
x1 = 1
x2 = 2
x3 = 3

:
function config($file) {
    return parse_ini_file($file);
}

, . .

PHP-


: 0.029, 0.111, 0.902

:
<?
return array (
  'x1' => '1',
  'x2' => '2',
  'x3' => '3',
);
?>

:
function config($file) {
    return include($file);
}

. return, . , , , . .

, INI-, . , , PHP-, .

XML-


: 0.062, 0.385, 3.911

:
<root>
  <x1>1</x1>
  <x2>2</x2>
  <x3>3</x3>
</root>

:
function config($file) {
    $r = array();
    $dom = new DOMDocument;
    $dom->load($file);
    foreach ($dom->firstChild->childNodes as $node) {
        if ($node->nodeType == XML_ELEMENT_NODE) {
            $r[$node->nodeName] = $node->firstChild->nodeValue;
        }
    }
    return $r;
}

: , , . , PHP- , return XML- ( , , ). . .

: NEW! 0.047, 0.276, 2.791

: NEW!
function config($file) {
    $r = array();
    foreach(simplexml_load_file($file) as $k => $v) {
        $r[$key] = strval($v);
    }
    return $r;
}

SimpleXML , , . , .


: 0.034, 0.250, 2.369

:
x1  1
x2  2
x3  3

:
function config($file) {
    $r = array();
    if ($F = fopen($file, "r")) {
        while (($line = fgets($F)) !== false) {
            list($k, $v) = explode("\t", $line, 2);
            $r[trim($k)] = trim($v);
        }
        fclose($F);
    }
    return $r;
}

, , . , , parse_ini_file, , . , .

: NEW! 0.036, 0.250, 2.213

: NEW!
function config($file) {
    $r = array();
    foreach (explode("\n", file_get_contents($file)) as $line) {
        list($k, $v) = explode("\t", $line, 2);
        $r[trim($k)] = trim($v);
    }
    return $r;
}

. , , .


: 0.011, 0.041, 0.309

:
a:3:{s:2:"x1";s:1:"1";s:2:"x2";s:1:"2";s:2:"x3";s:1:"3";}

:
function config($file) {
    return unserialize(file_get_contents($file));
}

— .

PHP- define'


: 0.045, 0.252, 2.404

:
<?
define("x1", "1");
define("x2", "2");
define("x3", "3");
?>

, , , . , , define .

JSON- NEW!


: 0.015, 0.057, 0.495

:
{"x1":"1","x2":"2","x3":"3"}

:
function config($file) {
    return json_decode(file_get_contents($file), true);
}

JSON . PHP , INI-, PHP. : , stdClass object.


, — . , . , .

— , . JSON- INI-, , .

, — PHP. , .

XML — . , , .

, define' , .


, ? . , PHP-, . PHP :

: 0.018, 0.046, 0.317

, .

:
function config($file) {
    $file_dat = "$file.dat";
    if (!file_exists($file_dat) || filemtime($file_dat) <= filemtime($file)) {
        $r = include($file);
        if ($F = fopen($file_dat, "w")) {
            fwrite($F, serialize($r));
            fclose($F);
        }
    } else {
        $r = unserialize(file_get_contents($file_dat));
    }
    return $r;
}

, , , , .

P.S. PHP- . , : . , . , PHP 4 5 (, , XML). , .
P.P.S. JSON.
P.P.P.S. . , , - .

Source: https://habr.com/ru/post/112402/


All Articles