我怎样才能得到一个程序在用C毫秒的执行时间? [英] How can I get the execution time of a program in milliseconds in C?

查看:144
本文介绍了我怎样才能得到一个程序在用C毫秒的执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我通过调用让我的程序的执行时间墙中的

Currently I'm getting the execution wall time of my program in seconds by calling:

time_t startTime = time(NULL);
//section of code
time_t endTime = time(NULL);
double duration = difftime(endTime, startTime);

是否有可能得到的毫秒墙时间?如果是的话怎么样?

推荐答案

如果你是一个POSIX十岁上下的机器上,使用的 函数gettimeofday() 代替; ,让你合理的便携性和微秒级。

If you're on a POSIX-ish machine, use gettimeofday() instead; that gives you reasonable portability and microsecond resolution.

略多深奥,而且在POSIX,是 clock_gettime() 功能,它给你纳秒的分辨率。

Slightly more esoteric, but also in POSIX, is the clock_gettime() function, which gives you nanosecond resolution.

在许多系统中,你会发现一个函数 FTIME(),其实返回在秒和毫秒的时间。然而,它在单一Unix规格(大致相同,POSIX)不再。您需要头< SYS / timeb.h>

On many systems, you will find a function ftime() that actually returns you the time in seconds and milliseconds. However, it is no longer in the Single Unix Specification (roughly the same as POSIX). You need the header <sys/timeb.h>:

struct timeb mt;
if (ftime(&mt) == 0)
{
     mt.time ... seconds
     mt.millitime ... milliseconds
}

这要追溯到第7版(或第7版)的Unix,至少,这样就已经很普遍。

This dates back to Version 7 (or 7th Edition) Unix at least, so it has been very widely available.

我也有在我的亚秒计时器code注倍()时钟(),它再次使用其他结构和标题。我也有关于使用时钟()的Windows 每秒(毫秒计时)与1000的时钟滴答,和旧的接口的GetTickCount()标注为需要在Windows 95,但不是在NT。

I also have notes in my sub-second timer code on times() and clock(), which use other structures and headers again. I also have notes about Windows using clock() with 1000 clock ticks per second (millisecond timing), and an older interface GetTickCount() which is noted as necessary on Windows 95 but not on NT.

这篇关于我怎样才能得到一个程序在用C毫秒的执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆