본문 바로가기

Life

!(a<b) 와 (a>=b) 의 시간 차이!


오늘 페북을 보다 마침 신선한 질문을 보게 되 직접 코드를 짜서 해보게 되었다.


질문은 제목과 같지만.

!(a<b) 가 더 빠를까 아니면 (a>=b) 가 더 빠를가 그것이 문제로다!


개인적으로 전자가 더 빠를거 같지만 실상 컴파일 하면 똑같게 될 지라 매번 비슷한 값으로

언젠 어느게 크고 언제 어느게 크고 할 줄 알았다.


테스트한 코드는 아래와 같다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
 
__declspec(nakedvoid main() {
    unsigned long t1, t2;
    int a, b;
 
    a = 20;
    b = 10;
 
    __asm { rdtsc }
    if (a >= b) {
        __asm {
            rdtsc
            mov t2, eax
        }
    }
 
    __asm { rdtsc }
    if (!(a < b)) {
        __asm {
            rdtsc
            mov t1, eax
        }
    }
 
    printf("%lu %lu", t1, t2);
}
cs

컴파일러는 VC140 (Visual Studio 2015 Update 3)

컴파일한 환경은 Windows 10 pro x86-64

빌드는 Debug, Release 둘 다 해 보았고, x86 바이너리로 컴파일 하였다.

옵션은 최적화 옵션은 다 제거하였고 시간측정은 rdtsc명령어를 사용하였다.


먼저 릴리즈 모드일 때 결과를 봐 보자.

1
2
3
4
5
6
7
8
9
10
C:\Users\zero>"C:\Users\zero\Documents\Visual Studio 2015\Projects\Test\Release\Test.exe"
3516528872 3516528821
C:\Users\zero>"C:\Users\zero\Documents\Visual Studio 2015\Projects\Test\Release\Test.exe"
2687723243 2687723195
C:\Users\zero>"C:\Users\zero\Documents\Visual Studio 2015\Projects\Test\Release\Test.exe"
620307864  620307813
C:\Users\zero>"C:\Users\zero\Documents\Visual Studio 2015\Projects\Test\Release\Test.exe"
2506852480 2506852429
C:\Users\zero>"C:\Users\zero\Documents\Visual Studio 2015\Projects\Test\Release\Test.exe"
2614860780 2614860729
cs

신기하게도 !(a<b) 가 50정도의 차이로 더 빠르게 측정된다.


다음은 디버그 모드일 때 결과를 봐 보자.

1
2
3
4
5
6
7
8
9
10
11
C:\Users\zero>"C:\Users\zero\Documents\Visual Studio 2015\Projects\Test\Debug\Test.exe"
4137175213 4137175162
C:\Users\zero>"C:\Users\zero\Documents\Visual Studio 2015\Projects\Test\Debug\Test.exe"
1936284920 1936284872
C:\Users\zero>"C:\Users\zero\Documents\Visual Studio 2015\Projects\Test\Debug\Test.exe"
321511877  321511829
C:\Users\zero>"C:\Users\zero\Documents\Visual Studio 2015\Projects\Test\Debug\Test.exe"
2389658557 2389658503
C:\Users\zero>"C:\Users\zero\Documents\Visual Studio 2015\Projects\Test\Debug\Test.exe"
282461296  282461245
 
cs

디버그 모드일 때도 비슷한 차이로 같은 결과가 나온다.


아래는 어셈 코드다. 두 코드다 분명히 jl 분기를 사용해 점프를 하게 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
.text:00401040                 mov     dword ptr [ebp-4], 14h
.text:00401047                 mov     dword ptr [ebp-8], 0Ah
.text:0040104E                 rdtsc
.text:00401050                 mov     eax, [ebp-4]
.text:00401053                 cmp     eax, [ebp-8]
.text:00401056                 jl      short loc_40105D
.text:00401058                 rdtsc
.text:0040105A                 mov     [ebp-10h], eax
.text:0040105D
.text:0040105D loc_40105D:                             ; CODE XREF: _main+16j
.text:0040105D                 rdtsc
.text:0040105F                 mov     ecx, [ebp-4]
.text:00401062                 cmp     ecx, [ebp-8]
.text:00401065                 jl      short loc_40106C
.text:00401067                 rdtsc
.text:00401069                 mov     [ebp-0Ch], eax
.text:0040106C
.text:0040106C loc_40106C:                             ; CODE XREF: _main+25j
.text:0040106C                 mov     edx, [ebp-0Ch]
.text:0040106F                 push    edx
.text:00401070                 mov     eax, [ebp-10h]
.text:00401073                 push    eax
.text:00401074                 push    offset _Format  ; "%lu %lu"
.text:00401079                 call    _printf
cs

혹시나 해서 두 알고리즘의 순서를 바꿔서 해 보았는데도 시간 차는 여전했다 ㄷㄷ


뭐 그래도 50정도 차이는 미세하니까 알고리즘을 짜는데도 영향을 안주겠죠 ㅋㅋ...