Qt测试计算时间
博客转载自:https://blog.csdn.net/lg1259156776/article/details/52325508
一、标准C和C++都可用
1. 获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t timer0 )。 精确到秒
#include <time.h>
#include <stdio.h>
int main()
{
time_t start ,end ;
double cost;
time(&start);
sleep(1);
time(&end);
cost=difftime(end,start);
printf("%f/n",cost);
return 0;
}
关于代码中的sleep函数,需要注意的是:
1)在windows下,为Sleep函数,且包含windows.h
2)关于sleep中的数,在Windows和Linux下1000代表的含义并不相同,Windows下的表示1000毫秒,也就是1秒钟;Linux下表示1000秒,Linux下使用毫秒级别的函数可以使用usleep。
2、clock_t clock(),clock函数获取的是计算机启动后的时间间隔,得到的是CPU时间,精确到1/CLOCKS_PER_SEC秒。
#include <time.h>
#include <stdio.h>
int main()
{
double start,end,cost;
start=clock();
sleep(1);
end=clock();
cost=end-start;
printf("%f/n",cost);
return 0;
}
注意与CPU振荡周期有关,所以不同CPU精度可能不一样。
3. 利用QTime,其精度为ms级
#include <QDebug> #include <QTime> QTime time; time.start(); function(); qDebug()<<time.elapsed()/1000.0<<"s";
4. Linux利用clock(),其精度为ms级
#include <QDebug> #include <sys/time.h> double time_Start = (double)clock(); function(); double time_End = (double)clock(); qDebug()<<(time_End - time_Start)/1000.0<<"s";
3. 利用windows.h函数,提精度为us级
#include <QDebug> #include <windows.h> LARGE_INTEGER litmp; LONGLONG Qpart1,Qpart2,Useingtime; double dfMinus,dfFreq,dfTime; //获得CPU计时器的时钟频率 QueryPerformanceFrequency(&litmp);//取得高精度运行计数器的频率f,单位是每秒多少次(n/s), dfFreq = (double)litmp.QuadPart; QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值 Qpart1 = litmp.QuadPart; //开始计时 function(); //待测试的计算函数等 QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值 Qpart2 = litmp.QuadPart; //终止计时 dfMinus = (double)(Qpart2 - Qpart1);//计算计数器值 dfTime = dfMinus / dfFreq;//获得对应时间,单位为秒,可以乘1000000精确到微秒级(us) Useingtime = dfTime*1000000; qDebug()<<dfTime<<"s";
4. 利用gettimeofday(),其精度为us级
#include <QDebug> #include <sys/time.h> struct timeval tpstart,tpend; float timeuse; gettimeofday(&tpstart,NULL); function(); gettimeofday(&tpend,NULL); timeuse=(1000000*(tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec)/1000000.0; qDebug()<<timeuse<<"s";
Qt测试计算时间的更多相关文章
- 【Qt开发】Qt测试计算时间
方法1 利用QTime,其精度为ms级 </pre><pre code_snippet_id="1852215" snippet_file_name=" ...
- qt DateTime 计算时间
qdatetime doc 获取当前时间 QDateTime t1 = QDateTime::currentDateTime(); qDebug() << t1.toString(&quo ...
- C++ 、Qt计算时间的方法
原文链接:https://blog.csdn.net/chy555chy/article/details/53405072 Qt计算时间的两种方法: QTime elapsed() : ms QTim ...
- 计算时间:一个C++运算符重载示例
Time类是一个用于计算时间的类,其原型如下:程序清单11.1 mytime0.h // mytime0.h -- Time class before operator overloading #if ...
- 【Python】我是如何使计算时间提速25.6倍的
我是如何使计算时间提速25.6倍的 我的原始文档:https://www.yuque.com/lart/blog/aemqfz 在显著性目标检测任务中有个重要的评价指标, E-measure, 需要使 ...
- flex安装时停在计算时间界面的解决办法
现象:安装FLEX BUILDER4.6时停在计算时间界面,过了一会后弹出安装失败的对话框. 环境:WIN7 解决: 1.下载AdobeCreativeCloudCleanerTool, 地址:htt ...
- 自学php找工作【二】 PHP计算时间加一天
最近几天在做一个项目,主要是将SQLserver数据到MySQL数据库,一个url跑一次 同步一次昨天的数据,由于很多数据需要同步,所以做了一个操作界面的,一个单纯跑url的 在其中涉及到了对于时间的 ...
- C语言 · 计算时间
算法提高 计算时间 时间限制:1.0s 内存限制:512.0MB 问题描述 给定一个t,将t秒转化为HH:MM:SS的形式,表示HH小时MM分钟SS秒.HH,MM,SS均是两位数,如 ...
- java为啥计算时间从1970年1月1日开始
http://www.myexception.cn/program/1494616.html ————————————————————————————————————————————————————— ...
随机推荐
- Win7+VS2010下配置WTL开发环境
一.今天Win7下刚装了VS2010,解压wtl81_12085.zip到C盘根目录,进入C:\wtl81_12085\AppWiz下,执行setup100.js提示向导安装成功. 在VS2010中新 ...
- 【.Net】Byte,Stream,File的转换
引言 文件的传输和读写通常都离不开Byte,Stream,File这个类,这里我简单封装一下,方便使用. 帮助类 public static class FileHelper { / ...
- java学习笔记 --- 网络编程(套接字)
1.Socket通信原理 Socket套接字概述: 网络上具有唯一标识的IP地址和端口号组合在一起才能构成唯一能识别的标识符套接字. 通信的两端都有Socket. 网络通信其实就是Socket间的通信 ...
- python编程实例-使用正则收集IP信息
#!/usr/bin/env python from subprocess import PIPE,Popen import re def getIfconfig(): p = Popen(['ifc ...
- 页面报错Uncaught SyntaxError: Unexpected identifier
错误描述:未捕获的语法错误:意想不到的标识符. 如图所示:检查之后发现是页面js内缺少“,”引起的.添加之后就OK了.
- 四、Jmeter--参数化
一.CSV 参数化 1.我们做性能测试需要并发多个用户,为了真实模拟用户行为,我们需要模拟多个不同的用户登录,这是我们就需要进行参数化.这里我们选择比较常用的参数化方法-CSV Data Set Co ...
- laravel 多个where的连接使用
在查询的时候需要用到多个where条件来查询 1.直接多个where连接 ->where()->where() 2.把查询条件 放到where数组$where中 然后 ->where ...
- Java基础--阻塞队列ArrayBlockingQueue
ArrayBlockingQueue是阻塞队列的一种,基于数组实现,长度固定,队尾添加,队首获取, 构造函数: ArrayBlockingQueue(int capacity) ArrayBlocki ...
- 机器学习:SVM(目标函数推导:Hard Margin SVM、Soft Margin SVM)
一.Hard Margin SVM SVM 的思想,最终用数学表达出来,就是在优化一个有条件的目标函数: 此为 Hard Margin SVM,一切的前提都是样本类型线性可分: 1)思想 SVM 算法 ...
- SQL修改日期时间型数据中的年月日
以下语句为更改 tevent表中的eventtime字段为2011-7-16当eventtime为2012-02-29时 update tevent set eventtime='2011-7-16 ...