php计算函数执行时间的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php计算函数执行时间的方法。分享给大家供大家参考。具体如下:

我们可以通过在程序的前后分别记录开始和结束时间,两个时间差就是程序的执行时间。

<?php
$long_str = "this is a test to see how much time md5 function takes to execute over this string";
// start timing from here
$start = microtime(true);
// function to test
$md5 = md5($long_str);
$elapsed = microtime(true) - $start;
echo "That took $elapsed seconds.\n";
?>

运行结果如下:

That took 7.1525573730469E-6 seconds.

php 计算函数执行时间的方法及获得微妙的方法

// 获得微妙方法
 function getMillisecond()
 {
   list($s1, $s2) = explode(' ', microtime());
   return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
 }

原理:分别记录函数开始时间和结束时间,然后时间差就是函数执行的时间

<?php
 $start_time = microtime(true);
for($i=1;$i<=1000;$i++){
echo $i.'<br>';
}
$end_time = microtime(true);
echo '循环执行时间为:'.($end_time-$start_time).' s';
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

探讨PHP中this,self,parent的区别详解

{一}PHP中this,self,parent的区别之一this篇面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能。利用OOP...

php中strtotime函数性能分析

最近在做一个游戏数据统计后台,最基础的功能是通过分析注册登录日志来展示用户数据。在公司内部测试,用户量很少,所以就没有发现什么性能问题。但是这两天一起放到真实的测试环境,用户量噌噌地就涌...

apache php模块整合操作指南

apache php模块整合操作指南

apache的版本: httpd-2.2.21-win32-x86-no_ssl php的版本: php-5[1].3.8-Win32-VC9-x86 (一) 准备工作 1.先找在D:/...

php中call_user_func函数使用注意事项

本文实例讲述了php中call_user_func函数使用注意事项。分享给大家供大家参考。具体分析如下: call_user_func函数的注意事项:parse error: synta...

PHP中strncmp()函数比较两个字符串前2个字符是否相等的方法

本文实例讲述了PHP中strncmp()函数比较两个字符串前2个字符是否相等的方法。分享给大家供大家参考,具体如下: PHP中的strncmp()函数用于比较两个字符串(区分大小写),可...