Home  >  Article  >  Backend Development  >  The method of returning an array directly is the slowest_PHP Tutorial

The method of returning an array directly is the slowest_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:36:57753browse

Caching files, the most commonly used one is probably return array();

Experiments have proven that this method is slower, try the following 3 files.

One. $arr.php directly returns an array

return Array
(
"db_host1" => 123,
"db_host2" => 123,
"db_host3" => 123,
"db_host4" => 123,
"db_host5" => 123,
"db_host6" => 123,
"db_host7" => 123,
"db_host8" = > 123,
"db_host9" => 123,
"db_host10" => 123,
"db_host11" => 123,
"db_host12" => 123,
"db_host13" => 123,
"db_host14" => 123,
"db_host15" => 123,
"db_host16" => 123
);
? >

Second, ini.php is an INI file


db_host1 =123
db_host2 =123
db_host3 =123
db_host4 =123
db_host5 =123
db_host6 =123
db_host7 =123
db_host8 =123
db_host9 =123
db_host10 =123
db_host11 =123
db_host12 =123
db_host13 =123
db_host14 =123
db_host15 =123
db_host16 =123
Third, str.php is the serialized array

return
a:16:{s:8:"db_host1";s:3:"123";s:8:"db_host2";s:3:"123" ;s:8:"db_host3";s:3:"123";s:8:"db_host4";s:3:"123";s:8:"db_host5";s:3:"123";s :8:"db_host6";s:3:"123";s:8:"db_host7";s:3:"123";s:8:"db_host8";s:3:"123";s:8 :"db_host9";s:3:"123";s:9:"db_host10";s:3:"123";s:9:"db_host11";s:3:"123";s:9:" db_host12";s:3:"123";s:9:"db_host13";s:3:"123";s:9:"db_host14";s:3:"123";s:9:"db_host15" ;s:3:"123";s:9:"db_host16";s:3:"123";};
?>

Conduct 10,000 tests on 3 files including the returned array

$t1 = microtime(true);

$file1 = ./arr.php;
$file2 = ./ini.php;
$file3 = ./str.php;

for($i=0; $i<10000; $i++){
//$arr = require $file1;
//$arr = parse_ini_file($file2);
$arr = unserialize(require $file3);
}

$t2 = microtime(true);
echo $t2-$t1;

Result:

arr.php 5.7820551395416

ini.php 5.3364160060883

str.php 5.5691919326782

Among them, the fastest ranked INI file is actually the INI file.

The second is serialization, and the slowest is returning the array directly

Conclusion:

The ini file is good and easy to write (can be considered)

Save the array, use serialization!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486611.htmlTechArticleTo cache files, the most commonly used method is probably return array(); Experiments have proven that this method is slower Yes, try checking the following 3 files. One. $arr.php directly returns the array?php return...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn