search
Homephp教程php手册追踪php代码性能瓶颈
追踪php代码性能瓶颈Jun 06, 2016 pm 08:11 PM
phpcodeperformancebottleneckExampleSimpletrack

写了一段很简单的代码示例,却发现运行起来速度很慢,出现性能问题的代码量并不大,排除了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

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

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

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

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

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

我们看到库函数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
.....

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

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

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

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

苹果手机丢了关机怎么追踪精确位置苹果手机丢了关机怎么追踪精确位置Mar 08, 2024 pm 02:30 PM

苹果手机丢了关机想要找回也是可以的,方法也很简单的,用户可以选择登录iCloud官网进行查找,或者是同样使用苹果手机的朋友,使用他的手机来查找你的iPhone。苹果手机丢了关机怎么追踪精确位置答:iCloud官网查找或者是借用他人iPhone设备查找1、用户发现自己的苹果手机丢了或者不见了,即使是关机状态也可以找到。2、用户直接登录iCloud官网,点击查找我的iPhone手机,注意一定要输入正确的账号。3、确保自己的账号适合丢失的手机的账号保持一致,这样才有机会找回手机。4、如果手机开机并连接

PHP邮件追踪功能:了解用户对邮件的行为和反馈。PHP邮件追踪功能:了解用户对邮件的行为和反馈。Sep 19, 2023 am 08:51 AM

PHP邮件追踪功能:了解用户对邮件的行为和反馈在现代社会中,电子邮件已经成为人们日常生活和工作中必不可少的一部分。对于企业来说,发送邮件是与客户进行沟通、推广产品或服务的重要方式之一。然而,一封邮件被发送出去后,我们如何知道它是否被收到、被读取,或者用户对邮件内容有何反应?这时,邮件追踪功能就显得尤为重要了。邮件追踪功能可以帮助我们了解用户对邮件的行为和反馈

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.