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

yipeiwu_com5年前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实现的自定义图像居中裁剪函数示例【测试可用】

PHP实现的自定义图像居中裁剪函数示例【测试可用】

本文实例讲述了PHP实现的自定义图像居中裁剪函数。分享给大家供大家参考,具体如下: 图像居中裁减的大致思路: 1.首先将图像进行缩放,使得缩放后的图像能够恰好覆盖裁减区域。(imagec...

二招解决php乱码问题

二招解决php乱码问题

php网页出现乱码一般是在建立数据库时用的编码和php网页的编码不同造成的, 用phpmyadmin建立的数据库如果你不指定编码他默认是latin1_swedish_ci 编码,既瑞典语...

如何用php获取程序执行的时间

在head.htm中加入,也就是在默认模版中添加“$stime=microtime(true); //获取程序开始执行的时间”复制代码 代码如下:<!--<?php$stim...

php mail to 配置详解

复制代码 代码如下: [mail function] ; For Win32 only. SMTP = mail3.focuschina.com smtp_port = 25 ; For...

PHP封装的数据库模型Model类完整示例【基于PDO】

本文实例讲述了PHP封装的数据库模型Model类。分享给大家供大家参考,具体如下: <?php //引入配置文件 include "../Config/...