Programming/C / C++

빠른 음수화

역시인생한방 2015. 2. 7. 20:05

변수값 음수화는 보통 양수값 앞에 -를 붙이거나 -1 을 곱하는데

문득 속도 차이가 얼마나 날지 궁금해서 테스트를 해보았다.


#include <iostream>
using namespace std;
#include <windows.h>

 

int main()
{
     int testCount = 1000000000;

 

     int x = 10000;
     int y = 0;

     DWORD startTime = 0;
     DWORD endTime = 0;

 

     startTime = GetTickCount();
     for( int i = 0; i < testCount; ++i )
     {
          y = -x;
     }
     endTime = GetTickCount();
     cout << "Test 1 : " << endTime - startTime << endl;

 

     startTime = GetTickCount();
     for( int i = 0; i < testCount; ++i )
     {
          y = x * -1;
     }
     endTime = GetTickCount();
     cout << "Test 2 : " << endTime - startTime << endl;

 

     return 0;
}

 

<결과>

Test 1 : 3422

Test 2 : 3500


그냥 -를 붙이는게 빨랐다