📜 ⬆️ ⬇️

After the previous post or About different methods of storing configs

This application simply must be configurable.

The news feed is not a feed if the admin panel does not allow you to specify how much news to display on the main one. A blog is not a blog, but a dull blog, if it is impossible to fine-tune a million parameters - from the title, to the different color of comments from girls and boys.

All these numerous settings, having gathered together, form a single whole - the configuration of your application, or, in other words - the config.
')
There are two problems with the config -
1. How and where to store it?
2. How and where to get it?
The relationship of these problems can be traced very clearly, so they can be reduced to one - “How to store configs?”

Ini files


In PHP, there is a rich toolkit for working with ini-files, consisting of as many as two functions: parse_ini_file () and parse_ini_string ().

Suppose that we have a config of 1000 variables. For simplicity of the situation, we will assume that we do not select sections in the ini-file.

For those who want to repeat the tests, here is an example of the simplest code for generating a test config file:
$f = fopen("./test.ini", "w");
for ( $i=1; $i<=1000; $i++ )
fwrite($f, "var_" . $i . " = \"" . md5(rand()) . "\"\n");
fclose($f);


Let's try to determine the time of access to an arbitrary variable from the config using a simple test code:
function getConfigVar($num) {

$config = parse_ini_file("./test.ini");
return $config["var_" . $num];

}

$start = microtime();

for ( $i=1; $i<=1000; $i++ )
$var = getConfigVar($i);

$end = microtime();

list($usec, $sec) = explode(" ", $start);
$start = (float)$usec + (float)$sec;

list($usec, $sec) = explode(" ", $end);
$end = (float)$usec + (float)$sec;

echo $end - $start;


After taking measurements on a test server 10 times and rounding the result, I got 2.67 seconds .

Serialized data in files


PHP offers us a special technology for storing arbitrary data of any kind in text files - serialization. For it, there are two special functions - serialize () converts data of any type into a string, and unserialize (), respectively, restores data from a string.

Create a test file with the following code:
for ( $i=1; $i<=1000; $i++ )
$data["var_" . $i] = md5(rand());
$f = fopen("./test.serial", "w");
fwrite($f, serialize($data));
fclose($f);

and accordingly change the getConfigVar function:
function getConfigVar($num) {

$config = unserialize(file_get_contents("./test.serial"));
return $config["var_" . $num];

}
, leaving the rest of the code unchanged.

After measuring 10 times and rounding the result, I got 1.48 seconds . Gain in time - almost 2 times!

Storage as Plain PHP


However, there is another way that was discussed in my previous topic. Configs can be stored as code in PHP, and for access, use the include () construct.
Let's see if it gives any performance gain.

Code to create a test config file:
$f = fopen("./test.conf", "w");
fwrite($f, "<?php\n");
for ( $i=1; $i<=1000; $i++ )
fwrite($f, "\$config['var_" . $i . "'] = \"" . md5(rand()) . "\";\n");
fwrite($f, "return \$config;\n?>");
fclose($f);

Pay attention to the last line of the file being created - “return $ config;”.
It would seem - why is the operator returning values ​​from a function? The secret is that in PHP the include statement also, like the function, can return the value defined in the return statement of the included file. This trick will allow us to do without global variables.

Testing:
function getConfigVar($num) {

$config = include("./test.conf");
return $config["var_" . $num];

}


The result is unexpected - 5.21 seconds . I personally thought that this method would be the fastest, but the miracle did not happen ...

... and if to cache?



And now let's try to take the slowest method and apply to it the simplest caching algorithm in memory. The test function will look like this:
function getConfigVar($num) {

if ( !isset($GLOBALS['config_cache']) )
$GLOBALS['config_cache'] = include("./test.conf");

return $GLOBALS['config_cache']["var_" . $num];

}

The result is striking - 0.054 seconds !

For reference, the first method after applying caching showed a result of 0.052 seconds, the second - 0.051 seconds.

I think that everyone will draw conclusions for himself.

The author is aware of some of the artificiality and tension of these tests. And hopes that they will simply serve as a tool for warm-up the intelligence of novice programmers, but not as a direct guide to action.

All examples given in the article are artificially invented and artificially simplified for a better understanding.

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


All Articles