PHP read/write Cookie efficiency analysis and performance optimization. Understand the execution time and efficiency of PHP read/write cookies, understand the impact of PHP read/write cookies on performance, and use PHP to read/write cookies properly in actual development, the analysis tool uses PEARBenchma to understand the execution time and efficiency of PHP read/write cookies, understand the impact of PHP read/write cookies on performance, and use PHP read/write cookies reasonably in actual development, the analysis tool uses the PEAR Benchmark_Iterate class. the main indicator is the execution time of the PHP program that reads and writes cookies.
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: |
|
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: |
|
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.
Analyze Cookie execution time and efficiency issues, understand the impact of PHP read/write cookies on performance, and use PHP to read and write cookies in actual development. the analysis tool uses PEAR Benchma...