PHP read-write cookie efficiency analysis and performance optimization _php tutorial

Source: Internet
Author: User
Tags benchmark http cookie pear php write
Master PHP read and write cookie execution time and efficiency issues, understand PHP read-write cookie on the performance of the impact, and in the actual development of reasonable use of PHP read and write cookies, analysis tools using Pear Benchmark_iterate class, The main focus is on the program execution time for PHP read and write cookies.

Directory

One, what is pear and benchmark class
Second, why to analyze PHP read-write cookie situation
Third, performance test code
Four, performance test results
Five, Performance test summary
Six, Setcookie function description
Seven, with performance test source code download
Resources

One, what is pear and benchmark class

Please refer to PHP performance Optimization series
Phase II PHP Performance Optimization Tool benchmark class debug execution time
First edition of PHP performance optimization preparation schematic pear installation

Second, why to analyze PHP read-write cookie situation

1, what is a cookie?
Cookies are data that some websites store on the user's local terminal in order to identify the user and track the session.

2,php and Cookies
PHP writes cookies through the built-in function Setcookie (), reads cookies through $_cookie global variables, and in real development, read and write cookies are one of the most important ways to interact with the user, and are used very frequently, There is a need to understand the performance and efficiency of PHP in read-write operation cookies.

Third, performance test code

Write three functions, namely, write cookies, read cookies, and read and write cookies, the code is as follows:

The code is as follows Copy Code
Require_once "benchmark/iterate.php";
$bench = new Benchmark_iterate;
function set () {
Setcookie ("TestCookie", Time (), Time () +3600, "/", "", 1);
}
function Get () {
return isset ($_cookie[' TestCookie ')? $_cookie[' TestCookie ': ';
}
function Cookie () {
Get ();
Set ();
}
$bench->run ("set");
$bench->run ("get");
$bench->run ("cookie");
$result = $bench->get ();
?>

Use the Benchmark_iterate class tool to call each function 50 times to get the average execution time of a PHP read-write cookie and generate a chart.

Four, performance test results

1,php Write Cookie Execution time


Diagram: Use PHP built-in function Setcookie () to send an HTTP cookie to the client, write the cookie execution time around 0.00072s note s to indicate seconds

2,php read the execution time of the cookie


Plot: Use global variable $_cookie to get client cookie value, read cookie execution time about 0.00051s

3,php execution time for read-write cookies

The cookie value is read first, then an HTTP cookie is sent to the client, and the execution time of the read-write cookie is around 0.00088s

Five, Performance test summary

A line chart makes it clear that the Setcookie () function sends an HTTP to the client Cookies require about 0.00072s execution time, that is, 0.7 milliseconds, PHP read cookie execution time is about 0.00051s, that is, 0.5 milliseconds, if the simultaneous read and write operation Cookie requires the execution time of 0.00088s, that is, about 0.8 milliseconds, these operations in seconds The times are basically executed very short, but please do not underestimate this value, Normal PHP built-in function execution time about 0.3 milliseconds, relative to read and write cookie execution time is relatively time-consuming, so in the actual development to minimize the use of such functions, fully consider the situation of reading and writing cookies, especially the situation of writing cookies!

The millisecond world is continuing, please follow the next PHP performance Optimization series.

Six, Setcookie function description

The Setcookie () function sends an HTTP cookie to the client.

The code is as follows Copy Code

BOOL Setcookie (String $name [, String $value [, int $expire = 0 [, String $path [, String $domain [, bool $secure = Fals e [, bool $httponly = FALSE]]]]

1,cookie is a variable that is sent to the browser by the server.
2,cookie is typically a small text file that is embedded in the user's computer by the server. This cookie is sent whenever the computer requests a page through a browser.
The name of the 3,cookie is specified as a variable of the same name. For example, if the cookie being sent is named "name", a variable named $user is automatically created, containing the value of the cookie.
4, you must assign a value to the cookie before any other output is sent.
5, if successful, the function returns True, otherwise false is returned.


http://www.bkjia.com/PHPjc/444718.html www.bkjia.com true http://www.bkjia.com/PHPjc/444718.html techarticle master PHP read and write cookie execution time and efficiency issues, understand PHP read-write cookie on the performance of the impact, and in the actual development of a reasonable use of PHP read and write cookies, analysis tools using pear Benchma ...

  • 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.