Home  >  Article  >  Backend Development  >  PHP reading and writing cookie efficiency analysis and performance optimization_PHP tutorial

PHP reading and writing cookie efficiency analysis and performance optimization_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:10:33861browse

Master the execution time and efficiency issues of PHP read and write cookies, understand the impact of PHP read and write cookies on performance, and rationally use PHP read and write cookies in actual development. The analysis tool uses the PEAR Benchmark_Iterate class, and the main indicators of concern are PHP read and write cookies. program execution time.

Table of contents

1. What are PEAR and Benchmark classes
2. Why should we analyze PHP reading and writing cookies
3. Performance test code
4. Performance test results
5. Performance test summary
6. Setcookie function description
7. Attached performance test source code download
Reference materials

1. What are PEAR and Benchmark classes

Please Refer to the PHP Performance Optimization Series
The second issue of PHP Performance Optimization Tools Benchmark class debugging execution time
The first issue of PHP Performance Optimization Preparation Illustration PEAR installation

2. Why analyze PHP read and write cookies Situation

1. What is Cookie?
Cookie refers to the data stored on the user's local terminal by some websites in order to identify the user's identity and perform session tracking.

2. PHP and Cookie
PHP writes cookies through the built-in function setcookie() and reads cookies through the $_COOKIE global variable. In actual development, reading and writing cookies is one of the most important ways to interact with users. , and it is also used very frequently, so some need to understand the performance and efficiency issues of PHP in reading and writing cookies.

3. Performance test code

Write three functions, namely write cookie, read cookie and read and write cookie. 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(50,"set");
//$bench->run(50,"get");
//$bench->run(50,"cookie");
$result = $bench->get();
?>
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(50,"set");//$bench-> ;run(50,"get");//$bench->run(50,"cookie");$result = $bench->get();?>

Use the Benchmark_Iterate class tool to call each function 50 times to obtain the average execution time of PHP reading and writing cookies and generate a chart.

Four. Performance test results

1. Execution time of writing cookies in PHP

PHP reading and writing cookie efficiency analysis and performance optimization_PHP tutorial
Illustration: Use PHP built-in function setcookie() to send the message to the client For an HTTP cookie, the execution time of writing the cookie is about 0.00072s. Note s means seconds

2. The execution time of PHP reading the cookie is
PHP reading and writing cookie efficiency analysis and performance optimization_PHP tutorial

Illustration: Use the global variable $_COOKIE to obtain Client cookie value, the execution time of reading Cookie is about 0.00051s

3, the execution time of PHP reading and writing Cookie
PHP reading and writing cookie efficiency analysis and performance optimization_PHP tutorial
Read the cookie value first, and then send one to the client HTTP cookie, the execution time of reading and writing cookies is about 0.00088s

5. Performance test summary

It can be clearly seen from the line chart that the setcookie() function needs to send an HTTP cookie to the client The execution time is about 0.00072s, which is 0.7 milliseconds. The execution time for PHP to read Cookie is about 0.00051s, which is 0.5 milliseconds. If the Cookie is read and written at the same time, the execution time required is 0.00088s, which is about 0.8 milliseconds. The execution time of these operations is basically very short in the era of seconds, but please do not underestimate this value. The execution time of normal PHP built-in functions is about 0.3 milliseconds. Relatively speaking, the execution time of reading and writing cookies is relatively time-consuming. , Therefore, in actual development, use such functions as little as possible, and fully consider the situation of reading and writing cookies, especially the situation of writing cookies!

The world of milliseconds continues, please pay attention to the next issue of the PHP performance optimization series.

6. Description of setcookie function

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

1. A cookie is a variable sent by the server to the browser.
The code is as follows
 代码如下 复制代码

bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

Copy code



bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ] ]]]]] )

2. Cookies are usually small text files that the server embeds into the user's computer. This cookie is sent each time the computer requests a page through the browser.
3. The name of the cookie is specified as a variable with the same name. For example, if the cookie being sent is named "name", a variable named $user will automatically be created containing the cookie's value. 4, the cookie must be assigned before any other output is sent.

5, if successful, the function returns true, otherwise it returns false.

http://www.bkjia.com/PHPjc/444718.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/444718.html
TechArticle
Master the execution time and efficiency issues of PHP read and write cookies, understand the impact of PHP read and write cookies on performance, and In actual development, PHP reading and writing cookies are used reasonably, and the analysis tool uses PEAR Benchma...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn