使用php-timeit估计php函数的执行时间
作者:可酷可乐 发布时间:2023-10-07 19:56:50
标签:php,timeit,php函数
废话不多说了,直接把我写的timeit函数分享给大家,具体内容如下:
/**
* Compute the delay to execute a function a number of time
* @param $count Number of time that the tests will execute the given function
* @param $function the function to test. Can be a string with parameters (ex: 'myfunc(123, 0, 342)') or a callback
* @return float Duration in seconds (as a float)
*/
function timeit($count, $function) {
if ($count <= 0){
echo "Error: count have to be more than zero";
return -1;
}
$nbargs = func_num_args();
if ($nbargs < 2) {
echo 'Error: No Funciton!';
echo 'Usage:';
echo "\ttimeit(count, 'function(param)')";
echo "\te.g:timeit(100, 'function(0,2)')";
return -1; // no function to time
}
// Generate callback
$func = func_get_arg(1);
$func_name = current(explode('(', $func));
if (!function_exists($func_name)) {
echo 'Error: Unknown Function';
return -1; // can't test unknown function
}
$str_cmd = '';
$str_cmd .= '$start = microtime(true);';
$str_cmd .= 'for($i=0; $i<'.$count.'; $i++) '.$func.';';
$str_cmd .= '$end = microtime(true);';
$str_cmd .= 'return ($end - $start);';
return eval($str_cmd);
}
测试一下自己写的一个求根算法与系统内置求根函数的执行时间,如下:
//取平方根
function sqrt_nd($num){
$value = $num;
while(abs($value*$value -$num) > 0.001){
$value = ($value + $num/$value)/2;
}
return $value;
}
print timeit(1000, 'sqrt_nd(5)');
print "\n";
print timeit(1000, 'sqrt(5)');
测试结果如下:
0.028280019760132
0.0041000843048096
可见,内置求根函数比自定义的求根函数快了6倍多~~
php中检测函数执行时间的功能使用的方法
PHP 中的 microtime() 函数可以实现
microtime() 函数返回当前 Unix 时间戳和微秒数。
microtime(get_as_float)
参数说明
get_as_float 如果给出了 get_as_float 参数并且其值等价于 TRUE,该函数将返回一个浮点数。
本函数仅在支持 gettimeofday() 系统调用的操作系统下可用。
例如:
<?php
$start_time = microtime(true);
for($i=1;$i<=1000;$i++){
echo $i.'<br>';
}
$end_time = microtime(true);
echo '循环执行时间为:'.($end_time-$start_time).' s';
?>


猜你喜欢
- 前言python2.x版本的字符编码有时让人很头疼,遇到问题,网上方法可以解决错误,但对原理还是一知半解,本文主要介绍 python 中字符
- 我就废话不多说了,大家还是直接看代码吧~b = torch.zeros((3, 2, 6, 6))a = torch.zeros((3, 2
- 产品通常分两种,一种是遵循现有用户习惯,一种是颠覆用户习惯。至于什么是用户习惯,你现在用右手还是左手操作鼠标,这就是你的习惯。很多公司团队专
- 1.LeNet介绍LeNet神经网络由深度学习三巨头之一的Yan LeCun提出,他同时也是卷积神经网络 (CNN,Convolutiona
- Method通过一条指令即可完成:os.system('所需指令')Note: os.system('所需指令
- 本文首先介绍在python3中print函数的应用,然后对比在pyhton2中的应用。(本文作者所用版本为3.6.0)首先我们通过help(
- 字符串字符串常用操作拼接字符串拼接字符串需要使用‘+’运算符可完成对多个字符串的拼接。如str =
- 详细:1.闵可夫斯基距离(Minkowski Distance)2.欧氏距离(Euclidean Distance)3.曼哈顿距离(Manh
- 时至期末,补习信息安全概论作业。恰巧遇古典密码学算法中的playfair算法和hill算法,用javascript语言实现起来是在有趣,边查
- SQL2005安装安装步骤安装Microsoft SQL Server 2005 数据库步骤:第一步:将Microsoft SQL Serv
- 使用正则表达式的几个步骤:1、用import re 导入正则表达式模块;2、用re.compile()函数创建一个Regex对象;3、用Re
- //创建组件 function createobj() { if (window.ActiveXObject) { return(new A
- 几乎每个程序都需要用到图片,在小程序中我们可以通过image组件显示图片。当然小程序也是可以上传图片的,微信小程序文档也写的很清楚。上传图片
- 对于数据实时同步,其核心是需要基于日志来实现,是可以实现准实时的数据同步,基于日志实现不会要求数据库本身在设计和实现中带来任何额外的约束。基
- 前言在Python开发中,有些情况下,我们可能面临在一台机器上同时安装多版本Python的需求。比如:有多个Python项目,每个项目依赖不
- 常用字段类型bit(0和1),datetime,int,varchar,nvarchar(可能含有中文用nvarchar) Varchar,
- 基本配置(萌新看,大佬请跳到下一节)1、创建项目点击Create New Project创建新的项目,点击Open打开已有的项目。先选择左侧
- Random库主要包含返回随机数的函数,主要用于普通的随机数生成的程序,如果对随机性有特殊要求,比如加密等,可以用函数os.urandom(
- 对于一个net开发这爬虫真真的以前没有写过。这段时间开始学习python爬虫,今天周末无聊写了一段代码爬取上海租房图片,其实很简短就是利用爬
- 问题:开发中常使用Navicat查询数据库,并修改数据库中的值。今天发现查询结果为只读,不能修改。一般连表查不能修改我是知道的,但是单表查居