PHP性能优化工具篇Benchmark类调试执行时间_PHP
这是PHP性能优化系列第二期,如何使用PEAR工具类Benchmark逐行获取代码或函数的执行时间。
工欲善其事,必先利其器!
如何安装PEAR和Benchmark
请参考PHP性能优化系列第一期 [PHP性能优化准备篇图解PEAR安装]
Benchmark工具类包说明
直接下载:http://pear.php.net/package/Benchmark/download
Benchmark工具类包共有三个文件,分别是Timer.php、Iterate.php和Profiler.php,三个工具类功能相同,只是侧重点不同,都是用于调试代码获取程序的执行时间。
1,Benchmark_Timer类原理与通过microtime函数获取微秒时间再比较前后两个时间值的差相同。
2,Benchmark_Iterate类用于调试函数的平均执行时间。
3,Benchmark_Profiler类用于统计代码和函数的执行时间以及函数的调用次数。
具体使用方法三个文件内都有详细的使用实例。
如何获取一行或一段代码的执行时间
1,通常使用microtime函数获取代码前后的微秒时间数再比较两个值的时间差,如下
但这种方法很有局限制,不能大范围的应用,而且每次都需要书写很多代码,适合于简单的调试。具体请查看PHP手册详细说明。
2,通过使用benchmark_Timer类获取代码前后执行的时间差,可以同时获取N行代码的执行时间,操作简单,只需要增加一个marker标记即可,请看下面Benchmark_Timer类的使用说明
如何使用Benchmark_Timer类
Benchmark_Timer类只需要在调试文件中增加Benchmark_Timer类初始化声明和marker标注,文件尾打印各个标注处的执行时间,实例如下
- require_once 'Benchmark/Timer.php';$timer = new Benchmark_Timer();$timer->start();$timer->setMarker("marker 01");usleep(1);$timer->setMarker("marker 02");usleep(2);$timer->setMarker("marker 03");usleep(3);$timer->stop();$timer->display();
打印结果有两种方法:
一种是表格输出方式,$timer->display(); 如下图
另外一种是手动var_dump或print_r打印,$timer->getProfiling();,print_r函数打印如下图
- array0 =>array'name' => string 'Start' (length=5)'time' => string '1265942405.31334800' (length=19)'diff' => string '-' (length=1)'total' => string '-' (length=1)1 =>array'name' => string 'marker 01' (length=9)'time' => string '1265942405.31374400' (length=19)'diff' => string '0.000396' (length=8)'total' => string '0.000396' (length=8)2 =>array'name' => string 'marker 02' (length=9)'time' => string '1265942405.31423000' (length=19)'diff' => string '0.000486' (length=8)'total' => string '0.000882' (length=8)3 =>array'name' => string 'marker 03' (length=9)'time' => string '1265942405.31519200' (length=19)'diff' => string '0.000962' (length=8)'total' => string '0.001844' (length=8)4 =>array'name' => string 'Stop' (length=4)'time' => string '1265942405.31623800' (length=19)'diff' => string '0.001046' (length=8)'total' => string '0.002890' (length=8)
结果说明
1,name表示标注名称,如上 包含两个特殊标注start和stop表示开始和结束,其次是自定义标注 marker 01 marker 02等
2,time表示当前的微秒时间
3,diff表示上一个标记到当前标记的执行时间,这个就是我们需要获取的时间差,没错,看的就是这个值。
4,total表示执行到当前的整个时间
如何使用Benchmark_Iterate类
Benchmark_Iterate类用于调试函数执行的平均时间,与Benchmark_Timer类不同在于可以多次调用同一个函数获取其执行时间的平均值,实例如下:
- require_once "Benchmark/Iterate.php";$bench = new Benchmark_Iterate;function test($i){ echo $i;}$bench->run(100,"test",10);var_dump($bench->get());
通过调用test函数100次获取平均执行时间,结果如下
- array1 => string '0.000486' (length=8)2 => string '0.000466' (length=8).............................(中间省略)99 => string '0.000479' (length=8)100 => string '0.000467' (length=8)'mean' => string '0.000476' (length=8)'iterations' => int 100
结果说明
1,每个数字表示每次调用的时间
2,mean表示函数执行的平均时间,如上调用100次test函数的平均时间为0.000476
3,iterations表示函数调用的次数
如何使用Benchmark_Profiler类
Benchmark_Profiler类用于统计函数的执行次数和执行时间等,实例如下:
- require_once 'Benchmark/Profiler.php';$profiler = new Benchmark_Profiler(TRUE);function myFunction() { global $profiler; $profiler->enterSection('myFunction'); //do something $profiler->leaveSection('myFunction'); return;}//do somethingmyFunction();//do more
结果如下
Benchmark_Profiler类在实际性能调试中使用并不多,因为还有比这个更好的工具,如xDebuger等,因此可直接忽略!
Benchmark 工具类在使用调试中针对逐行调试来分析程序性能问题非常实用,主要使用Benchmark_Timer类调试各代码段的时间点,以通过获取执行时间来优化程序提高代码的性能。这里就不再深入讨论,如果在使用的过程中有什么问题欢迎大家一起交流!
如果你发现这种逐行调试很累很辛苦,如果你想从整体上把握程序的性能情况,这个Benchmark类调试工具就不能满足你的需求,下期将讨论PHP性能调试工具xDebuger的安装与使用。
相关资料
microtime
(PHP 3, PHP 4, PHP 5)
microtime -- 返回当前 Unix 时间戳和微秒数
说明
mixed microtime ( [bool get_as_float] )
microtime() 当前 Unix 时间戳以及微秒数。本函数仅在支持 gettimeofday() 系统调用的操作系统下可用。
如果调用时不带可选参数,本函数以 "msec sec" 的格式返回一个字符串,其中 sec 是自 Unix 纪元(0:00:00 January 1, 1970 GMT)起到现在的秒数,msec 是微秒部分。字符串的两部分都是以秒为单位返回的。
如果给出了 get_as_float 参数并且其值等价于 TRUE,microtime() 将返回一个浮点数。
注意: get_as_float 参数是 PHP 5.0.0 新加的。
扩展资料
PHP Benchmark/Timer Class
PHP Benchmark
Benchmark and Optimize PHP Script Speed

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
