怎样计算C程序的运行时间
需要精确道毫秒。
不要使用WINDOWS下的函数,最好是C或C++的基本函数,以便在UNIX下运行。
多谢了!
问题点数:60、回复次数:15Top
1 楼Riemann()回复于 2002-11-18 16:47:26 得分 0
在程序开始与结束时分别读取系统时间,作差即可。Top
2 楼dcyu(Dd)回复于 2002-11-18 16:51:33 得分 10
一个例子:
#include <stdio.h>
#include <bios.h>
#include <time.h>
#include <math.h>
void main()
{
long i;
float a,b,c;
long bios_time;
double start,end;
bios_time = biostime(0, 0L);
start = bios_time / CLK_TCK;
a=234.32;b=187.26;
for(i=1;i<=100000000;i++)
{
c=a*b;
}
bios_time = biostime(0, 0L);
end = bios_time / CLK_TCK;
printf("Run time: %lf \n",end-start);
}
开始运行时和结束时插入抽样时间,然后将时间相减就可以了。Top
3 楼redsprite()回复于 2002-11-18 17:12:50 得分 0
接着问:如果是WINDOWS下,不用上面的BIOS.H文件,该如何做呢?Top
4 楼dcyu(Dd)回复于 2002-11-18 20:29:02 得分 0
你不是说“不要使用WINDOWS下的函数”吗?Top
5 楼kwest(为了Job,不得不学Linux)回复于 2002-11-19 09:31:12 得分 0
我一直用的是lcc-win32,它兼容ANSI C,可以自动显示出程序执行时间Top
6 楼redsprite()回复于 2002-11-19 15:02:51 得分 0
回:dcyu(Dd)
我现在调试在WINDOWS下,以后要换到UNIX下运行。所以各种情况都要考虑。
WINDOWS下BIOS.H文件没法用的说。
如果太麻烦就算了,分还是照给。Top
7 楼redsprite()回复于 2002-11-19 15:15:47 得分 0
多谢dcyu(Dd)!
Top
8 楼redsprite()回复于 2002-11-19 15:21:38 得分 0
多谢dcyu(Dd)!
我现在调试在WINDOWS下,以后要换到UNIX下运行。所以各种情况都要考虑。
WINDOWS下BIOS.H文件没法用的说。
Top
9 楼dxhdxh2k(dxhdxh)回复于 2002-11-19 15:30:03 得分 10
好象是getticktime() 或gettickcount(),具体你查一查Top
10 楼dcyu(Dd)回复于 2002-11-19 17:45:17 得分 0
bios.h是dos下的标准输入输出库,windows下不能用,以前记得有一个记时函数只要time.h库文件就行,但是忘了是什么了。unix下估计也不能使用biostime这个函数,你还是去查一查ANSI C的库函数吧。Top
11 楼asvaboy1980(蓝boy)回复于 2002-11-20 17:52:33 得分 0
gzTop
12 楼feng_zq(乔)回复于 2002-11-21 11:19:41 得分 20
这段代码是一个朋友给的,我一直用它测时间很好用
好像是直接读的中断
只要把它加在你代码里就可以了
#include <stdio.h>
#include <dos.h>
unsigned int GetTimeCount(void)
{
union REGS reg;
reg.h.ah = 0;
int86(0x1a, ®, ®);
return (reg.x.dx);
}
void main()
{
unsigned int StartTime, EndTime;
StartTime = GetTimeCount();
EndTime = GetTimeCount();
printf("************************");
printf("\nThe Tims is: %lfs", (EndTime-StartTime)/18.18);
printf("\n************************\n\n");
}Top
13 楼huzhangyou(信仰(http://www.libing.net.cn))回复于 2002-11-22 15:12:20 得分 20
/* 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 );
void main( void )
{
long i = 600000L;
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() )
;
}
vc下面通过Top
14 楼huzhangyou(信仰(http://www.libing.net.cn))回复于 2002-11-22 15:12:55 得分 0
/* 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 );
void main( void )
{
long i = 600000L;
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() )
;
}
vc下面通过Top
15 楼redsprite()回复于 2002-11-26 17:40:48 得分 0
多谢楼上的各位了。只是分太少了,加点吧。Top




