20億個 加法運算,需要 7.5秒
(高顯忠, sjgau4311@gmail.com, 2011-02-08 12:32)
1樓
#if 0
// 20億個 加法運算,需要 7.5秒
no= 2000000000, sum= -1973237248, dt= 7.516
no= 1000000000, sum= -243309312, dt= 3.656
no= 500000000, sum= -2083310976, dt= 1.656
no= 250000000, sum= -1532069568, dt= 0.828
no= 125000000, sum= 1795716256, dt= 0.375
no= 62500000, sum= 1538295888, dt= 0.188
no= 31250000, sum= -681355352, dt= 0.109
no= 15625000, sum= -1240174412, dt= 0.047
no= 7812500, sum= 1839393170, dt= 0.031
no= 3906250, sum= 1534566679, dt= 0.016
no= 1953125, sum= 384129951, dt= 0.000
no= 976562, sum= 95788347, dt= 0.000
no= 488281, sum= -1049672667, dt= 0.000
no= 244140, sum= -262479202, dt= 0.000
no= 122070, sum= -1139331107, dt= 0.000
no= 61035, sum= 1862666130, dt= 0.016
// 最小的時間計算的刻度是 0.016秒,
使用 ftime().
聽說,有更精確的方法?
no= 30517, sum= 465658903, dt= 0.000
no= 15258, sum= 116410911, dt= 0.000
no= 7629, sum= 29104635, dt= 0.000
no= 3814, sum= 7275205, dt= 0.000
no= 1907, sum= 1819278, dt= 0.000
no= 953, sum= 454581, dt= 0.000
no= 476, sum= 113526, dt= 0.000
no= 238, sum= 28441, dt= 0.000
no= 119, sum= 7140, dt= 0.000
no= 59, sum= 1770, dt= 0.000
no= 29, sum= 435, dt= 0.000
no= 14, sum= 105, dt= 0.000
no= 7, sum= 28, dt= 0.000
no= 3, sum= 6, dt= 0.000
no= 1, sum= 1, dt= 0.000
Press any key to continue
#endif
// ----------------------------------------------
#include <stdio.h>
#include <math.h>
#include <process.h>
#include <conio.h>
#include <sys/timeb.h>
#include <time.h>
#include "sj-01.h"
#include "sj-02.h"
// ----------------------------------------------
// for time1(), time2()
int main()
{
int no, sum, i, t1;
double dt;
no= (int) (20E8 + 0.5);
while (no > 0) {
time1(&t1);
sum= 0;
for (i=1;i<=no;i++) {
sum+= i;
}
time2(t1, &dt);
printf("no= %12d, sum= %12d, dt= %10.3lf \n",
no, sum, dt);
no/= 2;
}
return(0);
}// end of main()