Description: What is the best way to load one or many configuration data? Loading data from database, from an XML file, from INI file or from SESSIONS??? Below you can
see the results generated on the fly.
MEMORY INCREASE: how much memory consumes each method
EXECUTION TIME: how much time it needs for each method to be executed
Conclusions: As the results shows below, as many times as you run this tests saving configuration data in sessions is the fastest way with a slightly difference in memory usage which increases a bit compares to loading from INI file. Loading the configuration settings of your application in sessions saves resources and improves performance. But at what cost?
| DATA LOADING METHOD |
MEMORY INCREASE (bytes) |
EXECUTION TIME (seconds) |
| Database | 1520 | 0.002526 |
| INI | 1096 | 0.0001 |
| XML | 2216 | 0.000245 |
| Sessions | 1104 | 2.99999999998E-6 |
$connect = mysql_connect('localhost', 'username', 'password');
mysql_select_db('database', $connect);
$data = Array();
$q = mysql_query('select * from config');
$row = mysql_fetch_object($q);
while($row){
$data[$row->id][$row->name] = $row->value;
$row = mysql_fetch_object($q);
}
#Database:
CREATE TABLE `config` (
`name` varchar(150) DEFAULT NULL,
`value` varchar(250) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Data:
INSERT INTO `config` VALUES ('autoload', 'true');
INSERT INTO `config` VALUES ('show_captions', 'false');
INSERT INTO `config` VALUES ('language_preference', 'en');
INSERT INTO `config` VALUES ('allow_only_ip', '10.2.10.159');
INSERT INTO `config` VALUES ('templates_path', '/var/www/templates_c');
$ini_array = parse_ini_file("myconfig.ini", true);
$data = $ini_array['config'];
#INI File:
[config]
autoload = true
show_captions = false
language_preference = en
allow_only_ip = 10.2.10.159
templates_path = /var/www/templates_c
$data = $_SESSION['config'];
#Sessions setting up data
if(!isset($_SESSION['colors']) || !is_array($_SESSION['colors']) || sizeof($_SESSION['colors']) <= 0){
$_SESSION['config'] = Array('autoload' => 'true',
'show_captions' => 'false',
'language_preference' => 'en',
'allow_only_ip' => '10.2.10.159',
'templates_path' => '/var/www/templates_c');
}
$xml = new MyXml(); $xml->src = "myconfig.xml"; $xmlData = $xml->loadXml(); $data = $xmlData->application[0]; #XML File: <?xml version="1.0" encoding="UTF-8"?> <configuration> <application> <config> <name>autoload</name> <value>true</value> </config> <config> <name>show_captions</name> <value>false</value> </config> <config> <name>language_preference</name> <value>en</value> </config> <config> <name>allow_only_ip</name> <value>10.2.10.159</value> </config> <config> <name>templates_path</name> <value>/var/www/templates_c</value> </config> </application> </configuration>