Use hidef extension in PHP instead of define to improve performance, hidefdefine. In PHP, hidef extension is used instead of define to improve performance. hidefdefine requires a new constant to open the local config. PHP file, think of hidef tested a few years ago and apc to improve PHP using hidef extension instead of define to improve performance, hidefdefine
The website needs to add a new constant, open the local config. php file, and think of the hidef and apc programs tested several years ago to improve the define performance.
In my program, the development, testing, and production servers are configured separately. in terms of constants, an array is used to define all the constants to be defined, then, check whether the apc_load_constants function is available. if not, batch define. When apc is used, each time a constant is added, you need to modify $ key to take effect.
Now, after the testing and production server php is upgraded to 5.4, the opcode cache uses Zend opcache and does not install APC. Because it is useful to the apc user cache, apcu is additionally installed, which is similar to the apc usage. you do not need to change the program. Apcu does not support apc_load_constants and apc_define_constants, so apc cannot be used. Install the latest version hidef 0.1.13 and stable released on the official website for more than a year.
It took about 1000 ms to write a simple program to test define. For a medium-sized website (for example, if php runs 10000000 times a day), if 25 constants are defined on each page, 25/1000*2.8*700000 = 700 ms per day is required, that is, seconds. Hidef can save S of PHP running time in a day.
Let's take a look at the reading performance. we can test a constant for one million reads with a value of 1, which is 37 ms and 0.7 ms respectively. If 740 times a day and 20 constants are used on each page on average, it takes 700 seconds, while hidef is 14 seconds. well, another + seconds.
Saving the php running time of 1400 seconds a day may be insignificant, but it is always good. it is also a value attempt. after all, there is very little chance that the define parameter will change.
When the define parameter needs to be modified, modify the configuration file and reload php-fpm.
For more information about how to install hidef, see install and use the php extension hidef to improve define performance.
Search for "hidef" in Baidu ", ranking 3rd is a website that copies my blog posts: (I released my own php extension hidef to improve define performance, which was not found on the first three pages. Apparently Baidu has made some deviations in the original recognition.
Baidu searches for "hidef php". The website ranks first, the second is the official website, and the third is my website.
Gg searches for "hidef php". The first official website, 3rd is another original article, my fourth article. Gg recognition is good!
360 Search for "hidef php", the first copy site, the second is mine, and the third is original.
So with the birth of this article, you can see if you can arrange the first article or this article.
Sample code with the original configuration constant:
The code is as follows:
If (function_exists ('apc _ load_constants ')){
Function define_array ($ key, $ arr, $ case_sensitive = false ){
If (! Apc_load_constants ($ key, $ case_sensitive )){
Apc_define_constants ($ key, $ arr, $ case_sensitive );
}
}
} Else {
Function define_array ($ key, $ arr, $ case_sensitive = false ){
Foreach ($ arr as $ name => $ value ){
Define ($ name, $ value, $ case_sensitive );
}
}
}
$ Constants = array (
'Hx '=> 1,
'Blog _ url' => 'http: // www.bkjia.com /',
'Www _ url' => 'http: // www.bkjia.com /',
);
Define_array ('hx _ defined ', $ constants );
Code to test the define speed.
The code is as follows:
<? Php
$ T1 = microtime (1 );
$ Constants = array (
'Hx1 '=> 1,
'Hx2 '=> '2 ',
'H3' => '3 ',
'Hx4 '=> '4 ',
'Hx5 '=> '5 ',
'Hx6' => '6 ',
'Hx7' => '7 ',
'Hx8' => '8 ',
'Hx9' => '9 ',
'Hx10' => '10 ',
);
Function define_array ($ key, $ arr ){
Foreach ($ arr as $ name => $ value ){
Define ($ name. $ I, $ value );
}
}
For ($ I = 0; I I <100; $ I ++ ){
Define_array ($ I, $ constants );
}
$ T2 = microtime (1 );
Echo ($ t2-$ t1) * 1000;
// Read performance
$ T1 = microtime (1 );
For ($ I = 0; I I <10000; $ I ++ ){
$ T = hx1;
}
$ T2 = microtime (1 );
Echo ''. ($ t2-$ t1) * 1000;
$ T1 = microtime (1 );
For ($ I = 0; I I <10000; $ I ++ ){
$ T = HX;
}
$ T2 = microtime (1 );
Echo ''. ($ t2-$ t1) * 1000;
The Ghost website needs to add a new constant, open the local config. php file, and think of the hidef and apc improvements tested a few years ago...