PHP read/write Cookie efficiency analysis and performance optimization

Source: Internet
Author: User
Tags benchmark http cookie pear setcookie

Directory

1. What are PEAR and Benchmark classes?
2. Why should I analyze the PHP read and write cookies?
III. Performance test code
IV. Performance test results
5. Performance test summary
VI. setcookie function description
VII. Download performance test source code
References

1. What are PEAR and Benchmark classes?

See PHP performance optimization series
Phase ii php performance optimization tool Benchmark class debugging and execution time
Phase I PHP performance optimization preparation illustration PEAR installation

2. Why should I analyze the PHP read and write cookies?

1. What is Cookie?
Cookie refers to the data stored on the user's local terminal by some websites to identify users and track sessions.

2. PHP and Cookie
PHP writes a cookie through the built-in function setcookie () and reads the COOKIE through the global variable $ _ cookie. In actual development, read/write cookies are one of the most important ways to interact with users, it is also frequently used, because it is necessary to understand the performance and efficiency of the Cookie reading and writing operations in PHP.

III. Performance test code

Write three functions: write cookie, read cookie, and read/write cookie. The code is as follows:

The code is as follows: Copy code
<? Php
Require_once "Benchmark/Iterate. php ";
$ Tenant = new Benchmark_Iterate;
Function set (){
Setcookie ("TestCookie", time (), time () + 3600, "/", "", 1 );
}
Function get (){
Return isset ($ _ COOKIE ['testcooker'])? $ _ COOKIE ['testcookie ']: '';
}
Function cookie (){
Get ();
Set ();
}
$ Response-> run (50, "set ");
// $ Response-> run (50, "get ");
// $ Response-> run (50, "cookie ");
$ Result = $ response-> get ();
?>

Use the Benchmark_Iterate tool to call each function 50 times to obtain the average execution time of PHP read/write cookies and generate charts.

IV. Performance test results

1. Execution time of PHP Cookie writing


Illustration: use the PHP built-in function setcookie () to send an HTTP cookie to the client. The execution time of writing the Cookie is about 0.00072s. Note s to indicate the second.

2. Execution time of PHP read cookies


Illustration: use the global variable $ _ COOKIE to obtain the client cookie value. The execution time of the read Cookie is about 0.00051s.

3. PHP read/write Cookie execution time

Read the cookie value first, and then send an HTTP cookie to the client. The execution time of the read/write Cookie is about 0.00088s.

5. Performance test summary

The line chart shows that the execution time required by the setcookie () function to send an HTTP cookie to the client is about 0.00072s, that is, 0.7 milliseconds. The execution time for PHP to read the Cookie is about 0.00051s, that is, 0.5 milliseconds. If the Cookie execution time required for both read and write operations is 0.00088 s, that is, about 0.8 milliseconds, these operations will be executed very short in the second era, however, please do not underestimate this value. The execution time of normal PHP built-in functions is about 0.3 milliseconds, and the execution time of read/write cookies is relatively time-consuming. Therefore, in actual development, we should try to use such a function as little as possible to fully consider the situation of reading and writing cookies, especially when writing cookies!

The world is still going on in milliseconds. Please pay attention to the next PHP performance optimization series.

VI. 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 = false [, bool $ httponly = false])

1. cookie is a variable sent from the server to the browser.
2. Cookies are usually small text files embedded into users' computers by servers. This cookie is sent every time a computer requests a page through a browser.
3. The cookie name is a variable with the same name. For example, if the sent cookie is named "name", a variable named $ user is automatically created, containing the cookie value.
4. The cookie must be assigned a value before any other output is sent.
5. If the call succeeds, the function returns true; otherwise, the function returns false.

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.