php-timeit估计php函数的执行时间
首先,前段时间利用手头的日本VPS搭建了一个google代理,访问速度还行,分享给大家:
谷歌guge不行了,就打
谷歌:guge119.com
谷歌学术:scholar.guge119.com
有时候我们在PHP性能优化的时候,需要知道某个函数的执行时间,在Python中,有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-timeit估计php函数的执行时间的更多相关文章
- 使用timeit测试Python函数的性能
timeit是Python标准库内置的小工具,可以快速测试小段代码的性能. 认识timeit timeit 函数: timeit.timeit(stmt, setup,timer, number) 参 ...
- Python进阶量化交易专栏场外篇7- 装饰器计算代码时间
欢迎大家订阅<教你用 Python 进阶量化交易>专栏!为了能够提供给大家更轻松的学习过程,笔者在专栏内容之外已陆续推出一些手记来辅助同学们学习本专栏内容,目前已推出如下扩展篇: 在第一篇 ...
- 很小的一个函数执行时间调试器Timer
对于函数的执行性能(这里主要考虑执行时间,所耗内存暂不考虑),这里写了一个简单的类Timer,用于量化函数执行所耗时间. 整体思路很简单,就是new Date()的时间差值.我仅仅了做了一层简单的封装 ...
- 计时 timeit
python中的计时器:timeit timeit 通常在一段程序的前后都用上time.time(),然后进行相减就可以得到一段程序的运行时间,不过python提供了更强大的计时库:timeit #导 ...
- python第六天 函数 python标准库实例大全
今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...
- python中的计时器:timeit
python中的计时器:timeit timeit 通常在一段程序的前后都用上time.time(),然后进行相减就可以得到一段程序的运行时间,不过python提供了更强大的计时库:timeit #导 ...
- Direct3D Draw函数 异步调用原理解析
概述 在D3D10中,一个基本的渲染流程可分为以下步骤: 清理帧缓存: 执行若干次的绘制: 通过Device API创建所需Buffer: 通过Map/Unmap填充数据到Buffer中: 将Buff ...
- 浅谈Excel开发:五 Excel RTD函数
上文介绍了Excel中的UDF函数,本文介绍一下同样重要的RTD函数.从Excel 2002开始,Excel引入了一种新的查看和更新实时数据的机制,即real-time data简称RTD函数 ...
- Javascript函数节流
最近在做网页的时候有个需求,就是浏览器窗口改变的时候需要改一些页面元素大小,于是乎很自然的想到了window的resize事件,于是乎我是这么写的 <!DOCTYPE html> < ...
随机推荐
- Window的匿名Closing 事件
group.Closing += (sender, e) => { try { Code here } } catch (Exception ex) { Exception here } } ...
- membership source code
You can find their source code in codeplex at the ASP.NET source code. ExtendedMembershipProvider: h ...
- JAVA判断上传表单中file是否为空
<form action="update.do"method="post" enctype="multipart/form-data" ...
- Network 20Q--Q2 How does Google sell ad spaces?
在使用Google搜索的时候会发现,搜索出来的页面除了在左边显示搜索结果以外,还会页面的右边推荐一些广告.那么Google是怎么从这些广告挣钱以及广告商可以通过Google广告获得什么利益呢? Goo ...
- TPCC-UVA测试环境搭建与结果分析
一. 准备 操作系统 :Linux, 内核版本2.6 需要软件:tpccuva-1.2.3, postgresql-8.1.15, gnuplot-4.2.5. tccuva是实现标准TPC-C ...
- windows下删除服务的方法
删除的办法有两个: 办法一: 用sc.exe这个Windows命令 开始——运行——cmd.exe,然后输入sc就可以看到了.使用办法很简单: sc delete "服务名" (如 ...
- Oracle 用户、对象权限、系统权限
--================================ --Oracle 用户.对象权限.系统权限 --================================ 一.用户与模式 ...
- xhtml规范
在使用XHTML语言进行网页制作时,必须要遵循一定的语法规范.下面进行详细讲解,其中具体内容可以分为以下几点. 文档方面: 必须定义文档类型(DTD)和你的名字空间 标签方面: 所有标签均要小写,合理 ...
- mysql的锁--行锁,表锁,乐观锁,悲观锁
一 引言--为什么mysql提供了锁 最近看到了mysql有行锁和表锁两个概念,越想越疑惑.为什么mysql要提供锁机制,而且这种机制不是一个摆设,还有很多人在用.在现代数据库里几乎有事务机制,aci ...
- Oracle System密码忘记 密码修改、删除账号锁定lock
一下转自http://www.cnblogs.com/yjhrem/articles/2340149.html 运行cmd命令行 录入 sqlplus /nolog 无用户名登录 conn /as ...