Home >Backend Development >PHP Tutorial >How to use the ltrace tool to trace PHP library function calls

How to use the ltrace tool to trace PHP library function calls

WBOY
WBOYOriginal
2016-07-29 08:51:491235browse

The example in this article describes how to use the ltrace tool to trace PHP library function calls. I share it with you for your reference. The details are as follows:

Maybe you are already familiar with using strace to track system calls. Today I introduce a powerful tool for tracking library functions, ltrace

For example, I have this piece of PHP code

test.php:

<&#63;php
 $y = '1380';
 $arr = array();
 for($i = 0; $i < 2000; $i ++){
   $arr[] = "{$i}"; //故意用引号包起来设成字符串
 }
 for($i = 0; $i < 2000; $i ++){
   if(!in_array($y, $arr)) continue;
 }
&#63;>

ltrace -c /usr/local/php/bin/php test.php (-c means summary)

You will see the output as follows:

% time   seconds usecs/call   calls   function
------ ----------- ----------- --------- --------------------
95.02  7.417240     368   20146 strtol
2.15  7.160390     413   17316 memcpy
1.63  5.522641     240   22966 free
 0.67  2.275374   2275374     1 curl_global_cleanup
 0.54  2.235466     617   3618 __ctype_tolower_loc
 0.16  2.123547    1194   1778 strrchr
 0.17  1.532224     67   22836 malloc
 0.29  0.382083     67   5678 strlen

You can see that strtol almost uses up the execution time 95.02%, the bottleneck is found. And PHP will try to convert the string number into a long during the in_array() test, which will take a lot of time. Therefore, as long as all strings are converted into integers, the efficiency can be greatly improved.

ltrace is really a good tool

I hope this article will be helpful to everyone in PHP programming.

The above introduces the method of using the ltrace tool to track PHP library function calls, including trace content. I hope it will be helpful to friends who are interested in PHP tutorials.

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