Tracking php code performance bottlenecks

Source: Internet
Author: User
I wrote a simple code example, but found that the running speed was very slow, and the amount of code with performance problems was not large. After I/O problems were ruled out, I wrote a test code, the problem quickly recurs .? Php $ y1800; $ xarray (); for ($ j0; $ j [root @ xptrace] # timephptest. phpreal0m4.116suser0m4.070ssys0m0.

I wrote a simple code example, but found that the running speed was very slow, and the amount of code with performance problems was not large. After I/O problems were ruled out, I wrote a test code, the problem quickly recurs. ? Php $ y = "1800"; $ x = array (); for ($ j = 0; $ j [root @ xp trace] # time php test. phpreal 0m4. 116 suser 0m4. 070 ssys 0m0.

I wrote a simple code example, but found that the running speed was very slow, and the amount of code with performance problems was not large. After I/O problems were ruled out, I wrote a test code, the problem quickly recurs.

 

It can be seen that the time consumption is 4 seconds, and no valid information is obtained using the strace trace below.

[root@xp trace]# strace -ttt -o log.txt php test.php[root@xp trace]# more log.txt

We can only see that the latency between these two system calls is very large, but we don't know what we did? Unfortunately, the debugging tool in Linux includes strace and ltrace (of course, dtrace and ptrace ).

Reference: strace is used to track the system call or signal generation of a process, while ltrace is used to track the situation where the process calls a library function (? IBM developerworks ).

[root@xp trace]# ltrace -c php test.php

We can see that the strtol function of the library is called very frequently, which is too exaggerated. Then I checked this library function. Simply put, it is to convert the string into an integer, we can guess that the PHP engine has detected that this is a string-type number, so it is expected to convert them to a growth integer for comparison. This conversion process consumes too much time and we will execute it again:

[root@xp trace]# ltrace -e "strtol" php test.php(0, 0, 0xec4b00, -1, 0x1f25bc2)                                                        = 0x32dd221160strtol(0x1c9f9d0, 0, 10, 0, 0x7fffe64537f0)                                            = 8strtol(0x1c9f9b0, 0, 10, 0x7fffe6453880, 0x7fffe64537f0)                               = 30719strtol(0x1c9f9d0, 0, 10, 0x32ddb8ee88, 0x32ddb8d580)                                   = -9strtol(0x1c9f070, 0, 10, 371, 0x10119a0)                                               = 0strtol(0x1c9ea90, 0, 10, 370, 0x10119a0)                                               = 0strtol(0x1c9e580, 0, 10, 81, 0x1010c60)                                                = 1strtol(0x1c9fae0, 0, 10, 82, 0x1010c60)                                                = 0strtol(0x1ca0cf0, 0, 10, 26, 0x1010c60)                                                = 0strtol(0x1c9f7a0, 0, 10, 408, 0x1010c60)                                               = 0strtol(0x1c9e6d0, 0, 10, 432, 0x1010c60)                                               = 0strtol(0xcea563, 0, 10, 433, 0x1010c60)                                                = 0strtol(0xcea563, 0, 0, 440, 0x1010c60)                                                 = 0strtol(0x1c9f8f0, 0, 0, 72, 0x1010c60)                                                 = -1strtol(0xcea563, 0, 10, 298, 0x1010c60)                                                = 0strtol(0x1c9ee50, 0, 10, 5, 0x1010c60)                                                 = 1strtol(0x1c9fb80, 0, 10, 83, 0x1010c60)                                                = 1strtol(0x1c9fc30, 0, 0, 88, 0x1010c60)                                                 = 1024strtol(0x1c9fce0, 0, 10, 96, 0x1010c60)                                                = 0strtol(0x1c9fd90, 0, 10, 97, 0x1010c60)                                                = 0strtol(0x1c9f330, 0, 10, 98, 0x1010c60)                                                = 1strtol(0x1c9e4d0, 0, 10, 461, 0x1010c60)                                               = 0strtol(0x1ca05a0, 0, 10, 0, 0x1010c60)                                                 = 0strtol(0x1ca0650, 0, 10, 1, 0x1010c60)                                                 = 0strtol(0x1ca0700, 0, 10, 2, 0x1010c60)                                                 = 0strtol(0x1c9ec70, 0, 0, 8, 0x1010c60)                                                  = 0strtol(0x1ca03c0, 0, 10, 411, 0x1010c60)                                               = 1strtol(0x1ca0260, 0, 10, 409, 0x1010c60)                                               = 0strtol(0x1ca0310, 0, 10, 410, 0x1010c60)                                               = 0strtol(0x1ca0460, 0, 10, 412, 0x1010c60)                                               = 1strtol(0x1c9f130, 0, 10, 3, 0x1010c60)                                                 = 0strtol(0x1c9f1d0, 0, 10, 24, 0x1010c60)                                                = 0strtol(0x1c9e9f0, 0, 10, 369, 0x10119a0)                                               = 1strtol(0x1c9feb0, 0, 10, 25, 0x1010c60)                                                = 0strtol(0x1c9f3d0, 0, 10, 80, 0x1010c60)                                                = 0strtol(0x1c9ebd0, 0, 10, 413, 0x1010c60)                                               = 1strtol(0x1c9efe0, 0, 0, 48, 0x1010c60)                                                 = 17strtol(0x1c9f850, 0, 10, 0, 0)                                                         = 0strtol(0x1ca0d90, 0, 10, 457, 0x1010c60)                                               = 1strtol(0x1ca0e40, 0, 0, 160, 0x1010c60)                                                = 1000strtol(0x1ca0500, 0, 0, 432, 0x1010ec0)                                                = 1000strtol(0xd214c5, 0, 0, 520, 0x1010c60)                                                 = 64strtol(0xd37961, 0, 0, 584, 0x1010c60)                                                 = 1000strtol(0x1ca1250, 0, 10, 560, 0x1010c60)                                               = 1strtol(0x1c9f990, 0, 0, 0, 0)                                                          = 512strtol(0x1c9eb30, 0, 10, 0, 0)                                                         = 14strtol(0x1ca0f80, 0, 10, 459, 0x1010c60)                                               = 1strtol(0x1ca1030, 0, 10, 512, 0x1010c60)                                               = 0strtol(0xcea563, 0, 10, 460, 0x1010c60)                                                = 0strtol(0xd37a32, 0, 0, 24, 0x100ec20)                                                  = 16strtol(0xd37a49, 0, 0, 32, 0x100ec20)                                                  = 120strtol(0xd37a7c, 0, 0, 544, 0x1010c60)                                                 = 300strtol(0xcea563, 0, 10, 513, 0x1010c60)                                                = 0strtol(0x1c9ef30, 0, 10, 0, 0)                                                         = 30711strtol(0x1c9f700, 0, 10, 0, 0x1011d80)                                                 = 1(0x32dd221160, 0, 0, 0x32dd221160, 0)                                                  = 531783(0, 0, 0, 15, 0x42622d0)                                                               = 0x32dd221160(0x32dd221160, 0, 0, 0x32dd221160, 0)                                                  = 531720(0, 0, 0, 11, 0x7f9fa7b51b80)                                                          = 0x32dd221160(0x32dd221160, 0, 0, 0x32dd221160, 0)                                                  = 531719(0, 0, 0, 6, 0x9275f60)                                                                = 0x32dd221160(0x32dd221160, 0, 0, 0x32dd221160, 0)                                                  = 531897(0, 0, 0, 9, 0x7f9fa74b8338)                                                           = 0x32dd221160(0x32dd221160, 0, 0, 0x32dd221160, 0)                                                  = 531901(0, 0, 0, 3, 0x7f9fa72a8340)                                                           = 0x32dd221160(0x32dd221160, 0, 0, 0x32dd221160, 0)                                                  = 532944(0, 0, 0, 3, 0x7f9fa70641d0)                                                           = 0x32dd221160(0x32dd221160, 0, 0, 0x32dd221160, 0)                                                  = 525536(0, 0, 0, 3, 0x7f9fa6e5c680)                                                           = 0x32dd221160.....

Once we know the crux of the problem, we can solve a lot of problems. The simplest thing is to add the third parameter "true" to in_array, that is, to make a strict comparison and compare the types, this avoids PHP's self-made intelligent conversion type, and it is much faster to run.

[root@xp trace]# time php test.phpreal0m0.988suser0m0.959ssys0m0.024s

I wrote a simple code example, but found that the running speed was very slow, and the amount of code with performance problems was not large. After I/O problems were ruled out, I wrote a test code, the problem quickly recurs. It can be seen that the time consumption is 4 seconds, and no valid information is obtained using the strace trace below. We can only see that the latency between these two system calls is very large, but we don't know what we did? Unfortunately, the debugging tool in Linux includes strace and ltrace (of course, dtrace and ptrace ). Reference: strace is used to track the system call or signal generation of a process, while ltrace is used to track the situation where the process calls a library function (? IBM developerworks ). We can see that the strtol function of the library is called very frequently, which is too exaggerated. Then I checked this library function. Simply put, it is to convert the string into an integer, we can guess that the PHP engine has detected that this is a string-type number, so it is expected to convert them to a growth integer for comparison. This conversion process consumes too much time and we will execute it again: once we know the crux of the problem, we can solve a lot of problems. The simplest thing is to add the third parameter "true" to in_array, that is, to make a strict comparison and compare the types, this avoids PHP's self-made intelligent conversion type, and it is much faster to run.

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.