如何去计算一个程序运行的时间?

vn68214 2006-12-08 07:05:18
要精确到毫秒,time.h好像只有秒啊
...全文
643 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
不想低调 2006-12-12
  • 打赏
  • 举报
回复
mark
csShooter 2006-12-12
  • 打赏
  • 举报
回复
mark
hailongchang 2006-12-12
  • 打赏
  • 举报
回复
我们都知道,根本不可能精确测量某一个程序运行的确切时间,所谓的测量运行时间只是做一个近似的测量。我归纳总结的方法全部基于IA32及win32,Unix/Linux平台。

目前测量程序运行时间主要有两类方法,一种是基于计时器Timer的,另一种是基于计数器Counter的。
一:基于Timer的测量方法。

缺点:精度不够高,不能用于程序运行持续时间小于100ms的测量。
优点:准确性不是十分依赖于系统负载,并且在执行时间大于1s的程序上,与理论值之间的误差很低。
方法:在程序开始时读取计时器的内容,在程序终止前再次读取Timer的内容。主要的接口函数有:
Unix/Linux:
clock_t times(struct tms *buf);
//return value:系统自启动以来经过的时间滴答数,常数CLK_TCK表示每秒经过的时钟滴答数
//parameter:一个指向tms结构的指针
//使用该函数时要包含头文件<sys/times.h>
Win32:
DWORD GetTickCount(VOID)
//return value:the number of milliseconds that have elapsed since the system was started.
//使用时应包含<windows.h>,link阶段应链接 kernel32.lib

如果要编写可进行平台移植的代码,可以利用下面的函数:
clock_t clock(void)
//常数CLOCKS_PER_SEC保证将该函数返回的值格式化为秒数
//使用该函数时要包含头文件<time.h>

二:基于Counter的测量方法。

缺点:只能用汇编语言读取,不能保证通用性,在系统负载很大的情况下,将极大的影响准确性
优点:精度高,并且因为得到的是程序执行期间所经过的时钟周期数,所以可大致估算出在不同硬件平台上程序的执行时间。
方法:在IA32体系结构中,CPU内部有一个被称为“时间戳(TimeStamp)”的64位无符号数计数器,存储自cpu上电以来所经过的时钟周期数。

一:WIN32中有一个QueryPerformanceCouter函数读取的就是一个64位的计数器.

二:目前的compiler有的不支持RDTSC指令,如果在这种compiler下,可以利用__emit指令绕过compiler执行,应该在文件头加入:
#define CPUID __asm __emit 0fh __asm __emit 0a2h
#define RDTSC __asm __emit 0fh __asm __emit 031h
微软的C/C++编译器从6.0版开始支持CPUID和RDTSC指令,所以可以直接在程序中嵌入汇编代码,下面是一个简单示例:
#include<stdio.h>
int main()
{
unsigned int cycle,i;
__asm
{
CPUID
RDTSC
mov cycle,eax
}
for(i=0;i<10000;i++)
;
__asm
{
CPUID
RDTSC
sub eax,cycle
mov cycle,eax
}
printf("the program duration cycle = %d\n",cycle);
return 0;
}
由于基于counter的测量方法受影响的因素较多,主要是Context Switch和Instruction Cache的影响,所以高精度计时必须设法消除上述两种因素的影响,对Context Switch主要是采用在负载低的机器上多次计算求平均,而对Instruction Cache多采用提前载入需要测试代码段的Instruction,然后执行测量的方法.具体做法参见
http://www.cs.usfca.edu/~cruse/cs210/rdtscpm1-1.pdf
Computer System: A programmer's perspective(Chapter 7)

super1011 2006-12-12
  • 打赏
  • 举报
回复
用tiemGetTime啊,可以精确到millisecond
极速小王子 2006-12-11
  • 打赏
  • 举报
回复
gettimeofday,精确道微秒,还是用户太的
ILUYT 2006-12-10
  • 打赏
  • 举报
回复
晕,拿秒毫秒表来,嘿嘿!
「已注销」 2006-12-10
  • 打赏
  • 举报
回复
gettickcount
weiyong1011983 2006-12-10
  • 打赏
  • 举报
回复
我见过有人用取cpu周期求过,不过现在手头没有代码
楼主可以考虑一下
bobseadream 2006-12-09
  • 打赏
  • 举报
回复
直接用Unix下的time命令计算不就可以了吗?
owlling 2006-12-08
  • 打赏
  • 举报
回复
这个最常见的办法就是算这个程序进程的生命期了,。哈哈

==================================
欢迎访问我的个人主页:http://www.lingjie.net/
==================================
xuzheng318 2006-12-08
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <time.h>
#include <dos.h>

int main(void)
{
clock_t start, end;
start = clock();

delay(2000);

end = clock();
printf("The time was: %f\n", (end - start) / CLK_TCK);

return 0;
}
vn68214 2006-12-08
  • 打赏
  • 举报
回复
谢谢了,我试试
cjq87 2006-12-08
  • 打赏
  • 举报
回复
用ftime(struct timeb *)
sankt 2006-12-08
  • 打赏
  • 举报
回复
Calculates the wall-clock time used by the calling process.

clock_t clock( void );
Return Value
The elapsed wall-clock time since the start of the process (elapsed time in seconds times CLOCKS_PER_SEC). If the amount of elapsed time is unavailable, the function returns –1, cast as a clock_t.


Example
// crt_clock.c
/* This example prompts for how long
* the program is to run and then continuously
* displays the elapsed time for that period.
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void sleep( clock_t wait );

int main( void )
{
long i = 6000000L;
clock_t start, finish;
double duration;

/* Delay for a specified time. */
printf( "Delay for three seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" );

/* Measure the duration of an event. */
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds\n", duration );
}

/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}
Sample Output
Delay for three seconds
Done!
Time to do 6000000 empty loops is 0.1 seconds
sclarkca810619 2006-12-08
  • 打赏
  • 举报
回复
用WindowAPI能成吧?

69,377

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧