Home >Backend Development >PHP Tutorial >How to use the ltrace tool to trace PHP library function calls, ltrace library function_PHP tutorial
The example in this article describes how to use the ltrace tool to track PHP library function calls. Share it with everyone for your reference, the details are as follows:
Perhaps you are already familiar with using strace to track system calls. Today we introduce a powerful tool for tracking library functions, ltrace
For example, I have this piece of PHP code
test.php:
<?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; } ?>
ltrace -c /usr/local/php/bin/php test.php (-c means summary)
You will see the following output:
% 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 takes up almost 95.02% of the execution time, and 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.