您的位置:首页 > 编程语言 > C语言/C++

C/C++中计算程序运行时间

2013-08-30 10:45 363 查看
以前经常听人提起如何计算程序运行时间,给出一系列函数,当时没有注意,随便选了clock()最简单的方式进行计算。等到真正需要检测程序性能提升了多少,才发现这里面有很多要注意的地方。

最常用的的方式:

#include

time_t start = clock();

time_t end = clock();

printf("the running time is : %f\n", double(end -begin)/CLOCKS_PER_SEC);

clock()计算的是CPU执行耗时,注意是CPU!如果有多个核并行,最后的结果是每个CPU上运算时间的总和!想要精确到毫秒,可以double(end -begin)*1000/CLOCKS_PER_SEC

一般来说,只要求精确到秒的话,time是很好使的

#include <</span>stdio.h>

#include <</span>time.h>

int main(){

time_t t_start, t_end;

t_start = time(NULL) ;

sleep(3000);

t_end = time(NULL) ;

printf("time:
%.0f s\n", difftime(t_end,t_start)) ;

return 0;

}

如果要让程序休眠3秒,Windows使用Sleep(3000),Linux使用sleep(3),即Windows的Sleep接口的参数的单位是毫秒,Linux的sleep接口的参数的单位是秒。

如果需要精确到毫秒,以上程序就发挥不了作用,如果在Java要达到这要求就很简单了,代码如下所示:

下载: Time.java

public class Time {

public static void main(String[] args) {

try {

long startTime = System.currentTimeMillis();

Thread.sleep(3000);

long endTime = System.currentTimeMillis();

System.out.println("time: " + (endTime - startTime) + " ms");

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

通过Google找了一些资料后,发现C语言里没有标准的接口可以获得精确到毫秒的时间,都会调用到与操作系统相关的API,下面会分别介绍在Linux和Windows系统下的多种实现方法,希望对大家有帮助。


Linux系统

使用gettimeofday接口:

下载: gettimeofday.c

#include <</span>stdio.h>

#include <</span>sys/time.h>

int main() {

struct timeval start, end;

gettimeofday( &start, NULL );

sleep(3);

gettimeofday( &end, NULL );

int timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec -start.tv_usec;

printf("time:
%d us\n", timeuse);

return 0;

}

gettimeofday能得到微秒数,比毫秒还要更精确。

使用ftime接口:

下载: ftime.c

#include <</span>stdio.h>

#include <</span>sys/timeb.h>

long long getSystemTime() {

struct timeb t;

ftime(&t);

return 1000 * t.time + t.millitm;

}

int main() {

long long start=getSystemTime();

sleep(3);

long long end=getSystemTime();

printf("time:
%lld ms\n", end-start);

return 0;

}


Windows系统

使用GetTickCount接口:

下载: GetTickCount.c

#include <</span>windows.h>

#include <</span>stdio.h>

int main() {

DWORD start, stop;

start = GetTickCount();

Sleep(3000);

stop = GetTickCount();

printf("time:
%lld ms\n", stop - start);

return 0;

}

Windows系统下有些编译器使用printf输出64位整数参数要使用%I64d,比如VC。

使用QueryPerformanceX接口:

下载: QueryPerformance.c

#include <</span>windows.h>

#include <</span>stdio.h>

int main(){

LARGE_INTEGER li;

LONGLONG start, end, freq;

QueryPerformanceFrequency(&li);

freq = li.QuadPart;

QueryPerformanceCounter(&li);

start = li.QuadPart;

Sleep(3000);

QueryPerformanceCounter(&li);

end = li.QuadPart;

int useTime =(int)((end - start) * 1000 / freq);

printf("time:
%d ms\n", useTime);

return 0;

}

使用GetSystemTime接口:

下载: GetSystemTime.c

#include <</span>windows.h>

#include <</span>stdio.h>

int main(){

SYSTEMTIME currentTime;

GetSystemTime(¤tTime);

printf("time:
%u/%u/%u %u:%u:%u:%u %d\n",

currentTime.wYear,currentTime.wMonth,currentTime.wDay,

currentTime.wHour,currentTime.wMinute,currentTime.wSecond,

currentTime.wMilliseconds,currentTime.wDayOfWeek);

return 0;

}

这种方法没给出计算时间差的实现,只给出如何用GetSystemTime调用得到当前时间,计算时间差比较简单,根据年、月、日、时、分秒和毫秒计算出一个整数,再将两整数相减即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: