Home php教程 php手册 追踪php代码性能瓶颈

追踪php代码性能瓶颈

Jun 06, 2016 pm 08:11 PM
php code performance bottleneck Example Simple track

写了一段很简单的代码示例,却发现运行起来速度很慢,出现性能问题的代码量并不大,排除了IO问题以后,写了一段测试代码,果然问题很快重现。 ?php$y="1800";$x = array();for($j=0;$j[root@xp trace]# time php test.phpreal 0m4.116suser 0m4.070ssys 0m0.

写了一段很简单的代码示例,却发现运行起来速度很慢,出现性能问题的代码量并不大,排除了IO问题以后,写了一段测试代码,果然问题很快重现。

<?php $y="1800";
$x = array();
for($j=0;$j
[root@xp trace]# time php test.php
real 0m4.116s
user 0m4.070s
sys 0m0.028s
Copy after login

可以看出时间耗掉了4秒,下面用strace跟踪没拿到什么有效信息。

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

只看到这两次系统调用之间的延时非常大,却并不知道干了什么?一筹莫展了,幸好,Linux下的调试利器除了strace还有ltrace(当然还有dtrace,ptrace)。

引用:strace用来 跟踪一个进程的系统调用或信号产生的情况,而 ltrace用来 跟踪进程调用库函数的情况(via?IBM developerworks)。

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

我们看到库函数strtol的调用非常之频繁,太夸张了,然后我又查了一下这个库函数,简单的说就是把字符串转换成长整形,可以猜测PHP引擎已经检测到这是一个字符串型的数字,所以期望将他们转换成长整型来比较,这个转换过程中消耗了太多时间,我们再次执行:

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

知道了症结所在,我们解决的办法就很多了,最简单的就是为in_array加第三个参数为true,即变为严格比较,同时还要比较类型,这样避免了PHP自作聪明的转换类型,跑起来果然快多了。

[root@xp trace]# time php test.php
real	0m0.988s
user	0m0.959s
sys	0m0.024s
Copy after login

追踪php代码性能瓶颈 写了一段很简单的代码示例,却发现运行起来速度很慢,出现性能问题的代码量并不大,排除了IO问题以后,写了一段测试代码,果然问题很快重现。 可以看出时间耗掉了4秒,下面用strace跟踪没拿到什么有效信息。 只看到这两次系统调用之间的延时非常大,却并不知道干了什么?一筹莫展了,幸好,Linux下的调试利器除了strace还有ltrace(当然还有dtrace,ptrace)。 引用:strace用来 跟踪一个进程的系统调用或信号产生的情况,而 ltrace用来 跟踪进程调用库函数的情况(via?IBM developerworks)。 我们看到库函数strtol的调用非常之频繁,太夸张了,然后我又查了一下这个库函数,简单的说就是把字符串转换成长整形,可以猜测PHP引擎已经检测到这是一个字符串型的数字,所以期望将他们转换成长整型来比较,这个转换过程中消耗了太多时间,我们再次执行: 知道了症结所在,我们解决的办法就很多了,最简单的就是为in_array加第三个参数为true,即变为严格比较,同时还要比较类型,这样避免了PHP自作聪明的转换类型,跑起来果然快多了。追踪php代码性能瓶颈
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles