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 ————————————————————————————————————————————————————— ...
随机推荐
- 常用集合ArrayList浅度解析
首先,先看一下java中对ArrayList的定义代码: public class ArrayList<E> extends AbstractList<E> implement ...
- L118
The company needs to focus on its biggest clients.This article discussed the events that led to her ...
- HIVE-几道经典的hive题目
建表相关语句在此,具体的数据自己制作吧 create table student(Sno int,Sname string,Sex string,Sage int,Sdept string)row f ...
- mezzanine安装配置
ubuntu 14.04 cd /var/wwwmkdir testingcd testingvirtualenv venv --python=python3. venv/bin/activate p ...
- flask之Jinja2
Jinja2 is a library found at http://jinja.pocoo.org/; you can use it to produce formatted text with ...
- SVN 服务器端的搭建-及多仓库管理-OK
1.首先安装SVN这个软件 apt-get install subversion 2.在home目录下创建一个名为svn的文件夹(文件夹的名字随便起) mkdir /kk/svn/rda8501_co ...
- Spring Boot 集成RabbitMQ
在Spring Boot中整合RabbitMQ是非常容易的,通过在Spring Boot应用中整合RabbitMQ,实现一个简单的发送.接收消息的例子. 首先需要启动RabbitMQ服务,并且add一 ...
- POJ 1276 Cash Machine(单调队列优化多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38986 Accepted: 14186 De ...
- vector向量容器元素排序与查找
1.利用标准库函数sort()对vector进行排序 参考源码: #include <algorithm> #include <vector> vector<int> ...
- 分治思想的应用:C++实现快速排序和随机化的快速排序
分治思想的应用:C++实现快速排序和随机化的快速排序 原创 2014年09月08日 14:04:49 标签: 快速排序 / 随机化快速排序 / 排序算法 / 数据结构 947 1. 快速排序时冒泡排序 ...