当前位置: 代码网 > it编程>编程语言>C/C++ > C++下程序运行时间的四种常用计时方法总结

C++下程序运行时间的四种常用计时方法总结

2024年09月23日 C/C++ 我要评论
前言记录下当前时间start,调用程序fun(),再记录一下时间end。前后时间一减(start-end)就得到程序的运行时间了。首先介绍最常用的,但两种精度不是很高(>=10ms)的方法:cl

前言

记录下当前时间start,调用程序fun(),再记录一下时间end。

前后时间一减(start-end)就得到程序的运行时间了。

首先介绍最常用的,但两种精度不是很高(>=10ms)的方法:clock()和gettickcount()

一、clock()

c系统调用方法,所需头文件ctime/time.h,即windows和linux都可以使用。

1、clock()返回类型为clock_t类型

2、clock_t实际为long 类型, typedef long clock_t

3、clock() 函数,返回从 开启这个程序进程 到 程序中调用clock()函数 时之间的cpu时钟计时单元(clock tick)数(挂钟时间),返回单位是毫秒

4、可以用常量clocks_per_sec, 这个常量表示每一秒(per second)有多少个时钟计时单元

#include <time.h>   //引入头文件
int main()
{
clock_t start,end;   //定义clock_t变量
start = clock();     //开始时间

fun()  //需计时的函数

end = clock();   //结束时间
cout<<"time = "<<double(end-start)/clocks_per_sec<<"s"<<endl;  //输出时间(单位:s)
}

二、gettickcount()

gettickcount()是一个windows api,所需头文件为<windows.h>。

返回从操作系统启动到现在所经过的毫秒数(ms),精确度有限,跟cpu有关,一般精确度在16ms左右,最精确也不会精确过10ms。它的返回值是dword,当统计的毫妙数过大时,将会使结果归0,影响统计结果.

#include <windows.h>   //引入头文件
int main()
{
    dword t1,t2;
    t1 = gettickcount();

    fun()  //需计时的函数

    t2 = gettickcount();
    cout<<"time = "<<((t2-t1)*1.0/1000)<<endl;  //输出时间(单位:s)
}

接下来是两种高精度的计时方法:gettimeofday() 和 queryperformancecounter()

三、queryperformancecounter()

queryperformancecounter()是一个windows api,所需头文件为<windows.h>

这个函数返回高精确度性能计数器的值,它可以以微妙为单位计时.但是queryperformancecounter() 确切的精确计时的最小单位是与系统有关的,

所以,必须要查询系统以得到queryperformancecounter()返回的嘀哒声的频率. queryperformancefrequency() 提供了这个频率值,返回每秒嘀哒声的个数.

#include <windows.h>   //引入头文件
int main()
{
    large_integer t1,t2,tc;
    queryperformancefrequency(&tc);
    queryperformancecounter(&t1);

    fun()  //需计时的函数

    queryperformancecounter(&t2);
    time=(double)(t2.quadpart-t1.quadpart)/(double)tc.quadpart; 
    cout<<"time = "<<time<<endl;  //输出时间(单位:s)
}

四、gettimeofday()

gettimeofday() linux环境下的计时函数,int gettimeofday ( struct timeval * tv , struct timezone * tz ),gettimeofday()会把目前的时间由tv所指的结构返回,当地时区的信息则放到tz所指的结构中.

//timeval结构定义为:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
//timezone 结构定义为:
struct timezone{
int tz_minuteswest; /*和greenwich 时间差了多少分钟*/
int tz_dsttime; /*日光节约时间的状态*/
};

这个函数获取从1970年1月1日到现在经过的时间和时区(utc时间),(按照linux的官方文档,时区已经不再使用,正常应该传null)。

调用代码:

#include <sys/time.h>   //引入头文件
int main()
{
    struct timeval t1,t2;
    double timeuse;
    gettimeofday(&t1,null);

    fun();

    gettimeofday(&t2,null);
    timeuse = (t2.tv_sec - t1.tv_sec) + (double)(t2.tv_usec - t1.tv_usec)/1000000.0;

    cout<<"time = "<<timeuse<<endl;  //输出时间(单位:s)
}

还有一种c系统调用方法--time(),但是精度很低(秒级),不建议使用,这里就稍微带下用法。

    time_t start,stop;
    start = time(null);
    fun();
    stop = time(null);

附带三种计算python的代码块或程序的运行时间的方法

方法一

import datetime
start = datetime.datetime.now()
run_function():
    # do something
end = datetime.datetime.now()
print (end-start

方法二

import time
start = time.time()
run_function()
end = time.time()
print str(end)

方法三

import time
start = time.clock()
run_function()
end = time.clock()
print str(end-start)

其中,方法二的精度比较高。方法一基本上是性能最差的。这个其实是和系统有关系的。一般我们推荐使用方法二和方法三。我的系统是ubuntu,也就是linux系统,方法二返回的是utc时间。 在很多系统中time.time()的精度都是非常低的,包括windows。

总概来讲,在 unix 系统中,建议使用 time.time(),在 windows 系统中,建议使用 time.clock()。

总结

到此这篇关于c++下程序运行时间的四种常用计时方法的文章就介绍到这了,更多相关c++程序运行时间计时方法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com