는 I이 화학식 실현하는 프로그램 작성 :프로그램을 어떻게 최적화 할 수 있습니까?
의 PI = 1/N의 *의 써머 (4/(1 + ((I-0.5)/N)^2)
프로그램 코드 :
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
using namespace std;
const long double PI = double(M_PI);
int main(int argc, char* argv[])
{
typedef struct timeval tm;
tm start, end;
int timer = 0;
int n;
if (argc == 2) n = atoi(argv[1]);
else n = 8000;
long double pi1 = 0;
gettimeofday (&start, NULL);
for(int i = 1; i <= n; i++) {
pi1 += 4/(1 + (i-0.5) * (i-0.5)/(n*n));
}
pi1/=n;
gettimeofday (&end, NULL);
timer = (end.tv_usec - start.tv_usec);
long double delta = pi1 - PI;
printf("pi = %.12Lf\n",pi1);
printf("delta = %.12Lf\n", delta);
cout << "time = " << timer << endl;
return 0;
}
최적의 방법을 제시하는 방법
이 부분에 적은 부동 소수점 연산이있을 것이다 :
for(int i = 1; i <= n; i++) {
pi1 += 4/(1 + (i-0.5) * (i-0.5)/(n*n));
}
감사
"적은 작업"- 한 번 작성된 코드 나 지침의 줄은 무엇입니까? – Nim
Sergey, 스페이스 바가 작동하지 않습니까? – jrok
왜'printf'와'cout'을 합친 것입니까 – triclosan