Home > Back-end >  The bigger the for loop takes less??
The bigger the for loop takes less??

Time:09-27

#include
#include
#include
using namespace std;


Int main () {
For (long, n=2 * pow (10, 8); N<2 * pow (10, 12); N *=10)
{
Double pi_approx;
Float total;
Clock_t the begin and end;
Double sum=0.0;
Double factor=1.0;
The begin=clock ();
For (int k=0; KThe sum +=factor/(2 * k + 1);
}
Pi_approx=4 * sum;
End=clock ();
Total=(float) (end - the begin) * 1000/CLOCKS_PER_SEC;
Cout. Width (14);
Cout. Setf (ios: : left);
Cout. Precision (14);
cout}
}


Under different scale program as above, calculate the value of PI, and calculate all the time, it was found that why the size, the greater the time consuming less?

/root @ centos7 ~ # g + + pthred_chuanxing. CPP - o pthread_chuanxing - O2
[root @ centos7 ~] #./pthread_chuanxing
200000000 PI: 3.1415926485894 time: 360 ms
2000000000 PI: 3.1415926585052 time: 3569.9997558594 ms
20000000000 PI: 3.141592652102 time: 1200 ms
200000000000 PI: 3.1415926500684 time: 520 ms

When I perform 2000000000 directly, but how also not to the results
Ask bosses answer!!!!!
  • Related