PHP uses HiDef extensions instead of define to lift high performance _php

Source: Internet
Author: User
Keywords Php hidef extension instead of define improves
Tags apc apcu
The site needed a new constant, opened the local config.php file, and thought of the hidef that had been tested a few years ago and the plan for APC to improve define performance.
My program has a different configuration for development, testing, production server, in terms of constants used an array defines all the constants that need to be defined, and then detects whether there are apc_load_constants functions, no, batch define. With APC, you need to modify $key to take effect each time you add a constant.

Now that the test and production server PHP are upgraded to 5.4, the opcode cache uses the Zend Opcache, which no longer installs APC. Because it is useful to the APC user cache, it has an extra APCU, and APC usage, without changing the program at all. The APCU does not support apc_load_constants and apc_define_constants, so the APC solution 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 time of define, probably run 1000 times need 2.8ms. So for a medium-sized web site (for example, PHP runs 1000w times a day), if each page defines 25 constants, it takes about 700 seconds to 10000000*25/1000*2.8=700000ms every day. Almost using HiDef can save PHP running time by 700s a day.
Then look at the performance of reading, test read 1w A constant, the value is 1, respectively, 37ms and 0.7ms. Then if 1000w times a day, the average use of 20 constants per page, it takes 740 seconds, and the use of HiDef is 14 seconds, OK, another more than 700 seconds.
Save 1400 seconds a day PHP run time, perhaps is still insignificant, but always good, is also the value of the attempt, after all, define parameter changes in very little probability.
When the define parameter needs 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 PHP extension hidef, which I published myself to improve define performance, was not found on the first three pages. It is obvious that Baidu has identified some deviations from the original.

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

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


Sample code for the program with the original configuration constants:

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.bitsCN.com/',
' Www_url ' = ' http://www.bitsCN.com/',
);

Define_array (' hx_defined ', $constants);


The code with the test define speed is attached.

The code is 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;

  • Related Article

    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.