知識社群登入
好用的 time1(), time2() 副程式
by 高顯忠, 2010-11-19 18:49, 人氣(1395)

以下,20億個 加法運算,需要多少時間?

只看 主程式的部份,是不是很簡潔啊?


#if 0

no=   2000000000, sum=  -1973237248, dt= 7.828
 Press [Esc] for stop! other key for continue...

#endif
// ----------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>

#include <sys/timeb.h>
#include <time.h>
// ----------------------------------------------

#include "sj-01.h"
// ----------------------------------------------

void time1(int *t1)
{
struct _timeb timebuffer;
int ms, sec;
_ftime( &timebuffer );
ms= timebuffer.millitm;

sec= timebuffer.time;
sec%= (21L*86400L);// max. = 24.8 day, so ...
(*t1)= sec*1000 + ms;
}// end of time1()
// ----------------------------------------------

void time2(int t1, double *dt)
{
int t2;
time1(&t2);
(*dt)= ((double) (t2 - t1))/1000.0;
// (*dt) must >= 0
if ((*dt) < 0) {
(*dt)+= (21L*86400L);
}
}// end of time2()
// ----------------------------------------------

int main()
{
// 20億個 加法運算,需要多少時間?

int no= (int) (20E8 + 0.5);// 四捨五入
int sum, i, t1;
double dt;

time1(&t1);
sum= 0;
for (i=1;i<=no;i++) {
sum+= i;
}
time2(t1, &dt);
printf("no= %12ld, sum= %12ld, dt= %.3lf\n", no, sum, dt);
pause();
return 0;
}// end of main()


討論
編號標題回應最後發表
2688
1
0