레이블이 plus인 게시물을 표시합니다. 모든 게시물 표시
레이블이 plus인 게시물을 표시합니다. 모든 게시물 표시

2014년 4월 17일 목요일

덧셈연산과 곱셈연산의 속도차이

평소에도 궁금하고 프로젝트의 성능을 좌지우지하는 중요한 사항이기때문에
테스트를 해봤습니다.

소스코드는 다음과 같습니다.
<<덧셈연산>>
#include <iostream>
#include <boost/chrono.hpp>

using namespace std;

int main()
{
double a = 1.0;
double b = 1.1;
boost::chrono::high_resolution_clock::time_point start, end;
start = boost::chrono::high_resolution_clock::now();

for(int i=1; i< 5001; i++)
{
a +=b;
}
end  = boost::chrono::high_resolution_clock::now();
cout<<end - start<<endl;

cout<< "value is "<< a << endl;

return 0;
}

<<곱셈연산>>
#include <iostream>
#include <boost/chrono.hpp>

using namespace std;

int main()
{
double a = 1.0;
double b = 1.1;
boost::chrono::high_resolution_clock::time_point start, end;
start = boost::chrono::high_resolution_clock::now();

for(int i=1; i< 5001; i++)
{
a *=b;
}
end  = boost::chrono::high_resolution_clock::now();
cout<<end - start<<endl;

cout<< "value is "<< a << endl;

return 0;
}


제 환경에서는 결과가 다음과 같습니다.
회차   덧셈 곱셈
1 4226 6943
2 4226 7244
3 4528 7245
4 4226 7244
5 4528 7244

덧셈이 빠른가 봅니다.. 이유는 멀까요? 부동소숫점 연산이라 곱셈이 빠를것 같았는데.. 
예상 외입니다.