tic/toc/cputime测试时间
cputime测试代码运行时间可能不及tic/toc准确是众所周知的事情。本文并非旧话重提,而是期望起到抛砖引玉的效果,从而找到cputime与tic/toc内在的区别。望不吝赐教!
用tic/toc测试如下代码
- tic
- pause(10);
- t=toc
复制代码
会发现matlab处于busy状态并持续10秒,然后输出:
t =
10.0007
然而用如下cputime测试:
- t1=cputime;
- pause(10);
- t2=cputime;
- t=t2-t1
复制代码
会发现matlab也会处于busy状态并持续10秒,然后输出:
t =
0.1875
为何差别如此巨大? 于是本人阅读了help,发现了:
Using tic and toc Versus the cputime Function
Although it is possible to measure performance using the cputime function, it is recommended that you use the tic and toc functions for this purpose exclusively. It has been the general rule for CPU-intensive calculations run on Microsoft Windows machines that the elapsed time using cputime and the elapsed time using tic and toc are close in value, ignoring any first time costs. There are cases however that show a significant difference between these two methods. For example, in the case of a Pentium 4 with hyperthreading running Windows, there can be a significant difference between the values returned by cputime versus tic and toc.
原来Mathworks自己也是推荐用tic toc,而非cputime,他们也发现了超线程奔四CPU用cputime测试时间跟tic toc差距很大,根据我个人的理解,这应该跟CPU多核间运算分配(多核CPU情况)、多线程之间运算分配(超线程CPU),以及CPU内部运算优化等多种因素有关。cputime测试的并非代码实际执行时间,而是CPU实际运行的时间。通过CPU内部多核间任务调度、线程间调动、cpu自身优化(如被称为turbo睿频的软超频技术等)等,每个cpu实际运行时间会大大低于代码执行时间,因而也就出现了cputime测试时间大大低于tic/toc的时间。比如你的电脑是双核同时还支持超线程,那么你用cputime测试的时间最大不会超过代码运行时间的1/4,实际情况中恐怕会更小。
然而,tic/toc是直接测试代码实际执行时间,不论cpu有多少内核、多少线程、做何种优化,tic/toc都会给出代码实际运行时间的总和,因此tic/toc可以比较准确反映代码的真实运行时间。正因为如此,Mathworkss自己也是推荐用tic toc,而非cputime。
如今是多核的时代,并且Intel绝大多数cpu同时支持超线程,稍微高端一定的cpu都支持turbo技术,那么cputime测试的时间恐怕只会更加不准,所以,还是用tic toc把
以上纯属个人观点,欢迎批评指正,以期进步!
tic/toc/cputime测试时间的更多相关文章
- 关于MATLAB中的tic toc的问题
关于MATLAB中的tic toc的问题 其一) MATLAB实际单位时间计时函数的具体应用,在编写程序时,经常需要获知代码的执行实际时间,这就需要在程序中用到计时函数,matlab中提供了以下三种方 ...
- [T-ARA][Tic Tic Toc]
歌词来源:http://music.163.com/#/song?id=22704478 Tic Tic Toc RA Tic Tic Toc RA [Tic Tic Toc RA Tic Tic T ...
- MATLAB 秒表函数 tic toc 计算程序运行时间
若需要测试出程序运行所需时间,或对不同的运行方式所需时间进行对比,则可利用秒表函数tic和toc.Tic函数启动定时器,第一个紧跟它的toc函数终止定时器并报告此时定时器的流逝时间.其语法如下: t ...
- [Matlab] tic toc
tic Start a stopwatch timer. tic and TOC functions work together to measure elapsed time. tic, ...
- How to have matlab tic toc in C++?
Reprinted form: https://stackoverflow.com/questions/13485266/how-to-have-matlab-tic-toc-in-c/1348558 ...
- Matlab中tic和toc用法
简单地说,tic和toc是用来记录matlab命令执行的时间 tic用来保存当前时间,而后使用toc来记录程序完成时间. 两者往往结合使用,用法如下: tic operations toc 显示时间单 ...
- MATLAB——时间,日期及显示格式
一.日期和时间 1.生成指定格式日期和时间 标准日期格式 2.获取当前时间的数值 >> datestr(now,) ans = -- :: >> datestr(now,'yy ...
- Matlab编程基础
平台:Win7 64 bit,Matlab R2014a(8.3) “Matlab”是“Matrix Laboratory” 的缩写,中文“矩阵实验室”,是强大的数学工具.本文侧重于Matlab的编程 ...
- 【转载】让你的MATLAB代码飞起来
原文地址:http://developer.51cto.com/art/201104/255128_all.htm MATLAB语言是一种被称为是"演算纸"式的语言,因此追求的是方 ...
随机推荐
- J2EE 第二阶段项目(八)
类别统计差不多完成了! 还有个地区统计了!
- aspx后缀映射成html
1.网站的配置文件添加如下代码: <configuration> <configSections> <section name="RewriterConfig& ...
- json方法
http://penghuaiyi.iteye.com/blog/1922632 package com.yd.web.util; import java.lang.reflect.Type; imp ...
- [C:\Users\Administrator\.IntelliJIdea2016.1\system\tomcat\Unnamed_demo_2\work\Catalina\localhost\demo\org\apache\jsp\index_jsp.java]
http://www.oschina.net/question/1444338_2146454?sort=time
- 通用 PE 工具箱1.9.6(XP内核)by Uepon(李培聪)
通用 PE 工具箱1.9.6(XP内核)by Uepon(李培聪)官网:http://hi.baidu.com/uepon?page=21.8版论坛帖子:http://bbs.wuyou.net/fo ...
- SVN操作手册
目 录 第1章 简介 1 第2章 SVN服务端 2 2.1 安装VisualSVN 2 2.2 VisualSVN服务 3 2.3 版本库 4 2.3.1 创建版本库 ...
- linux笔记:用户管理命令和用户组管理命令
用户管理命令 命令名称:useradd功能:添加用户(添加完后不能立即使用,必须用passwd修改用户密码后才能使用)用法:useradd [选项] 用户名选项参数:-u 手工指定用户的UID-d 手 ...
- 理论与实践中的 C# 内存模型,第 2 部分
转载自:https://msdn.microsoft.com/zh-cn/magazine/jj883956.aspx 这是介绍 C# 内存模型的系列文章的第二篇(共两篇). 正如在 MSDN 杂志十 ...
- java复用和传参
java复用和传参的三种方法总结: (1) 直接在一个类中调用函数 : 1 package test1; 2 3 public class a 4 { 5 public static void get ...
- js(引用类型和setTimeout scope)
题目是群中小伙伴出的. var a = [1,2,3]; c= a //todo 限制条件 c 不能出现在 = 左边 console.log(a) console.log(c) console.log ...