Using HiDef extensions in PHP instead of define for high performance _php techniques

Source: Internet
Author: User
Tags apc apcu first row

The site needs to add a new constant, open the local config.php file, think about the hidef that was tested a few years ago and the APC upgrade define performance.
My program has a different configuration for development, testing, production servers, and in constant terms, we use an array that defines all the constants that need to be defined, and then detects if there is a apc_load_constants function, not a batch define. With APC, you need to modify the $key to take effect every time you add a constant.

Now the test, production server PHP has been upgraded to 5.4, the opcode cache using the Zend Opcache, no longer install APC. Because it is useful to APC user cache, the additional APCU, and APC usage, completely without the change program. The APCU does not support apc_load_constants and apc_define_constants, so the APC scheme is unusable. To the official website installed the latest version of HiDef 0.1.13,2012-7-12 released stable, more than a year.

Write a simple program to test the define time, probably run 1000 times need 2.8ms. So for a midsize web site (for example, PHP runs 1000w times a day), if you define 25 constants per page, you probably need to 10000000*25/1000*2.8=700000ms for 700 seconds a day. Almost using HiDef can save 700s of PHP uptime in one day.
Then look at the performance of the read well, the test read 1w times a constant, the value is 1, respectively, 37ms and 0.7ms. So if it's 1000w times a day, it takes 740 seconds to use 20 constants per page, and using HiDef is 14 seconds, OK, another more than 700 seconds.
1400 seconds a day to save PHP run time, perhaps is still insignificant, but always good, but also the value of the attempt, after all, define parameter changes in the probability of very little.
When the define parameters need to be modified, modify the configuration file, and then overload the next php-fpm, just fine.

HiDef specific installation methods see: Installation and use of PHP extension hidef to improve define performance

In Baidu search "HiDef", ranked 3rd is a copy of my blog site: ( The installation and use of the PHP extension hidef that I released myself to improve define performance were not found on the first three pages. It is obvious that Baidu has made some deviations from the original recognition.

Baidu search "HiDef php", that Web site ranked first, the second is the official website, my third.
GG Search "HiDef php", the first official website, the 3rd is another original, my in the fourth. GG recognition is good!

360 Search "HiDef php", the first copy station, the second mine, the third another article original.
So with the birth of this article, see if you can help the first row up or this row up also.


Program sample code with original configuration constants:

Copy Code code 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) {
   & nbsp;                define ($name, $ Value, $case _sensitive);
               }
           }
       }

$constants = Array (
' HX ' => 1,
' Blog_url ' => ' http://www.jb51.net/',
' Www_url ' => ' http://www.jb51.net/',
);

Define_array (' hx_defined ', $constants);


Code with test define speed.

Copy Code code as follows:

<?php

$t 1 = microtime (1);

$constants = Array (
' Hx1 ' => 1,
' Hx2 ' => ' 2 ',
' Hx3 ' => ' 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 <100; $i + +) {
Define_array ($i, $constants);
}


$t 2 = microtime (1);
Echo ($t 2-$t 1) *1000;

Read performance
$t 1 = microtime (1);
For ($i =0 $i <10000; $i + +) {
$t = hx1;
}
$t 2 = microtime (1);
Echo '. ($t 2-$t 1) *1000;

$t 1 = microtime (1);
For ($i =0 $i <10000; $i + +) {
$t = HX;
}
$t 2 = microtime (1);
Echo '. ($t 2-$t 1) *1000;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.