博客转载自: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测试计算时间的更多相关文章

  1. 【Qt开发】Qt测试计算时间

    方法1 利用QTime,其精度为ms级 </pre><pre code_snippet_id="1852215" snippet_file_name=" ...

  2. qt DateTime 计算时间

    qdatetime doc 获取当前时间 QDateTime t1 = QDateTime::currentDateTime(); qDebug() << t1.toString(&quo ...

  3. C++ 、Qt计算时间的方法

    原文链接:https://blog.csdn.net/chy555chy/article/details/53405072 Qt计算时间的两种方法: QTime elapsed() : ms QTim ...

  4. 计算时间:一个C++运算符重载示例

    Time类是一个用于计算时间的类,其原型如下:程序清单11.1 mytime0.h // mytime0.h -- Time class before operator overloading #if ...

  5. 【Python】我是如何使计算时间提速25.6倍的

    我是如何使计算时间提速25.6倍的 我的原始文档:https://www.yuque.com/lart/blog/aemqfz 在显著性目标检测任务中有个重要的评价指标, E-measure, 需要使 ...

  6. flex安装时停在计算时间界面的解决办法

    现象:安装FLEX BUILDER4.6时停在计算时间界面,过了一会后弹出安装失败的对话框. 环境:WIN7 解决: 1.下载AdobeCreativeCloudCleanerTool, 地址:htt ...

  7. 自学php找工作【二】 PHP计算时间加一天

    最近几天在做一个项目,主要是将SQLserver数据到MySQL数据库,一个url跑一次 同步一次昨天的数据,由于很多数据需要同步,所以做了一个操作界面的,一个单纯跑url的 在其中涉及到了对于时间的 ...

  8. C语言 · 计算时间

    算法提高 计算时间   时间限制:1.0s   内存限制:512.0MB      问题描述 给定一个t,将t秒转化为HH:MM:SS的形式,表示HH小时MM分钟SS秒.HH,MM,SS均是两位数,如 ...

  9. java为啥计算时间从1970年1月1日开始

    http://www.myexception.cn/program/1494616.html ————————————————————————————————————————————————————— ...

随机推荐

  1. 分布式_理论_04_ 3PC

    一.前言 五.参考资料 1.分布式理论(四)—— 一致性协议之 3PC 2.分布式理论(四) - 3PC协议 3.

  2. Git_学习_07_ 推送修改到远端

    一.操作流程 多人协作时,若自己的本地代码有了修改,想提交自己的代码,就需要按照以下步骤操作: 1.确认修改正确 使用以下命令,查看有哪些是自己未提交的代码 git status 2.拉取远程最新代码 ...

  3. hdu-2544-最短路(Floyd算法模板)

    题目链接 题意很清晰,入门级题目,适合各种模板,可用dijkstra, floyd, Bellman-ford, spfa Dijkstra链接 Floyd链接 Bellman-Ford链接 SPFA ...

  4. L116

    7. You will discover surprising new ideas that are interesting and engaging Reading introduced me to ...

  5. 树莓派(Linux)与镜像源

    树莓派学习笔记--修改树莓派软件源 1. linux 镜像源文件 >> vim /etc/apt/sources.list 可在树莓派官网 http://www.raspbian.org/ ...

  6. dedecms列表页文章有图调用缩略图 无图留空或自定义图片的方法!

    默认情况下,织梦的文章列表页会调用出当前栏目下的文章列表,并且调用出每个文章的缩略图:如果文章本身就有图,会调用出一张小图,如果没有,则会显示默认的织梦图片.这种处理方式有时候比较影响美观,其实可以修 ...

  7. Arc083_F Collecting Balls

    传送门 题目大意 给定$N$,在$(1,0),(2,0)......(N,0)$和$(0,1),(0,2)...(0,N)$上都有$1$个机器人,同时给定$2N$个坐标$(x,y),x,y\in[1, ...

  8. 排列(加了点小set就过了,哈哈哈)

    Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. 输入描述: 1 2 3 4 1 1 2 3 0 1 2 3 0 0 0 0输出 ...

  9. getParameter() getInputStream()和getReader() 区别

    我们经常用servlet和jsp, 经常用request.getParameter() 来得到数据. request.getParameter() request.getInputStream() r ...

  10. 【转】Tomcat和Weblogic的区别

    J2ee开发主要是浏览器和服务器进行交互的一种结构.逻辑都是在后台进行处理,然后再把结果传输回给浏览器.可以看出服务器在这种架构是非常重要的. 这几天接触到两种Java的web服务器,做项目用的Tom ...