In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What are the functions and commands related to PHP performance analysis and their performance? in view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
We discuss the performance of PHP from three aspects: PHP is an interpretive language, a dynamic language and an underlying implementation. In this article, we will delve into the micro level of PHP, and let's see what we may need to pay attention to and improve the performance of PHP in the process of using and writing code. What are the functions and commands related to PHP performance analysis? what is the performance of PHP?
What are the functions and commands related to PHP performance analysis
1.1. Time measurement function
We usually use the time () function, but the number of seconds is returned. For the internal performance analysis of a section of code, the accuracy of seconds is not enough. So use the microtime function. The microtime function can return two forms, one as a string and the other as a floating-point number. It is important to note, however, that by default, the precision returned is only 4 decimal places. To achieve higher accuracy, we need to configure precision.
The following is the result of using microtime.
$start=microtime (true)
Echo$start. / n
$end=microtime (true)
Echo$end. / n
Echo ($end-$start). "/ n"
The output is:
Bash-3.2#phptime.php
1441360050.3286
1441360050.3292
0.00053000450134277
Add a line in front of the code:
Ini_set ("precision", 16)
The output is:
Bash-3.2#phptime.php
1441360210.932628
1441360210.932831
0.0002031326293945312
In addition to microtime internal statistics, you can also use getrusage to obtain the duration of the user state. In the actual operation, the time command is often used to calculate the running time of the whole program. By running or modifying the code many times, we can get different lengths of time to get the difference in efficiency. Specific usage is: timephptime.php, then after the completion of the program, whether or not the normal end of the exit, there will be relevant statistics.
Bash-3.2#timephptime.php
1441360373.150756
1441360373.150959
0.0002031326293945312
Real0m0.186s
User0m0.072s
Sys0m0.077s
Because the performance issues discussed in this article tend to analyze the gap and trend after millions of calls, in order to avoid some time statistics code in the code, we mostly use the time command later.
1.2. Memory usage related functions
There are two functions for analyzing memory usage: memory_get_usage and memory_get_peak_usage, the former can obtain the memory currently used by the program at the point in time of the call, and the latter can obtain the memory used so far during peak periods. The memory used is in bytes.
$base_memory=memory_get_usage ()
Echo "Hellopies Worldwide Association"
$end_memory=memory_get_usage ()
$peak_memory=memory_get_peak_usage ()
Echo$base_memory, "/ t", $end_memory, "/ t", ($end_memory-$base_memory), "/ t", $peak_memory, "/ n"
The output is as follows:
Bash-3.2#phphelloworld.php
Hello,world!
As you can see, even if only one sentence is output in the middle of the program, coupled with variable storage, 168 bytes of memory are consumed.
For the same program, different versions of PHP use different memory, even very different.
$baseMemory=memory_get_usage ()
ClassUser
{
Private$uid
Function__construct ($uid)
{
$this- > uid=$uid
}
}
For ($iTuno / iselfrankobj)
The code is as follows:
$baseMemory=memory_get_usage ()
ClassUser
{
Private$uid
Function__construct ($uid)
{
$this- > uid=$uid
}
}
For ($iTuno / iselfrankobj)
If ($I% 5000)
{
Echosprintf ('% 6d bytes/n'), memory_get_usage (), "bytes/n"
}
}
Echo "peak:", memory_get_peak_usage (true), "bytes/n"
At this time, let's take a look at the memory usage. The main part of the middle table is the memory usage, in bytes.
2. What is the performance of PHP?
Let's verify some common performance differences based on Mini Program.
2.1.Use echo or print
In some suggested rules, it is recommended to use echo instead of print. It is said that print is a function and echo is a grammatical structure. In fact, this is not the case, print is also a grammatical structure, similar grammatical structure, there are many, such as list, isset, require and so on. However, there is a performance difference between the following PHP versions of PHP7. The following two codes are available:
For ($iDevo _ nulls
Real0m0.234s
User0m0.159s
Sys0m0.073s
[root@localhostphpperf] # timephpecho.php > / dev/null
Real0m0.203s
User0m0.130s
Sys0m0.072s
[root@localhostphpperf] # timephpecho.php > / dev/null
Real0m0.203s
User0m0.128s
Sys0m0.075s
The efficiency gap in the PHP5.3 version is more than 10%. In versions above PHP5.4, there is little difference, as follows, the running efficiency in PHP7.
[root@localhostphpperf] # timephp7echo.php > / dev/null
Real0m0.151s
User0m0.088s
Sys0m0.062s
[root@localhostphpperf] # timephp7echo.php > / dev/null
Real0m0.145s
User0m0.084s
Sys0m0.061s
[root@localhostphpperf] # timephp7echo1.php > / dev/null
Real0m0.140s
User0m0.075s
Sys0m0.064s
[root@localhostphpperf] # timephp7echo1.php > / dev/null
Real0m0.146s
User0m0.077s
Sys0m0.069s
Like some optimization guidelines on the front end of browsers, there are no particularly general principles, and the rules vary according to different circumstances and versions.
2.2, require or require_once?
In some general optimization rules, it is mentioned that it is recommended to use require_once instead of require, now that require_once detects duplicates, while require does not require duplicate tests.
Require_once is slightly slower than require in the inclusion of a large number of different files. But require_once detection is an in-memory behavior, that is, even if there are several files that need to be loaded, detection is only a comparison in memory. Every time require is reloaded, the analysis is read from the file system. So require_once will be better than require. Let's also use an example to see.
Str.php
Global$str
$str= "Chinahasalargepopulation"
Require.php
For ($iSuppli
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.