號稱,可以精確到 ms 的 ftime()
(高顯忠, sjgau4311@gmail.com, 2010-11-17 07:03)
1樓
其實,是騙人的。
我可以證明,他無法精確到 ms.
(高顯忠, sjgau4311@gmail.com, 2010-11-19 11:12)
2樓
#if 0
no= 10, sum= 55, dt= 0.000
no= 20, sum= 210, dt= 0.000
no= 40, sum= 820, dt= 0.000
no= 80, sum= 3240, dt= 0.000
no= 160, sum= 12880, dt= 0.000
no= 320, sum= 51360, dt= 0.000
no= 640, sum= 205120, dt= 0.000
no= 1280, sum= 819840, dt= 0.000
no= 2560, sum= 3278080, dt= 0.000
no= 5120, sum= 13109760, dt= 0.000
no= 10240, sum= 52433920, dt= 0.000
no= 20480, sum= 209725440, dt= 0.000
no= 40960, sum= 838881280, dt= 0.000
no= 81920, sum= -939483136, dt= 0.000
no= 163840, sum= 536952832, dt= 0.016
no= 327680, sum= -2147319808, dt= 0.000
no= 655360, sum= 327680, dt= 0.000
no= 1310720, sum= 655360, dt= 0.015
no= 2621440, sum= 1310720, dt= 0.032
no= 5242880, sum= 2621440, dt= 0.046
no= 10485760, sum= 5242880, dt= 0.079
no= 20971520, sum= 10485760, dt= 0.093
no= 41943040, sum= 20971520, dt= 0.219
no= 83886080, sum= 41943040, dt= 0.313
no= 167772160, sum= 83886080, dt= 0.625
no= 335544320, sum= 167772160, dt= 1.453
no= 671088640, sum= 335544320, dt= 2.718
no= 1342177280, sum= 671088640, dt= 6.360
Press any key to 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%= (21*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()
{
int t1, no= 10, sum, i;
double dt;
while (no > 0) {
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);
no*= 2;
}
return 0;
}// end of main()
(高顯忠, sjgau4311@gmail.com, 2010-11-19 18:33)
3樓
ftime() 縮然有提供 ms 的資訊,
但是,受限於 作業系統,每隔一定的時間 間隔,
才更新ㄧ次系統的時間,
所以,參考上面的程式的輸出,
可以發現,ftime() 的精密度,
只有到 15 - 16 個 ms