๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/๋„คํŠธ์›Œํฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

Fast Byte Order ์—„์ฒญ ์˜ค๋ž˜๋œ ๋‚ด์šฉใ…‹

๐ŸŽฎinspirer9 2021. 2. 1. 00:39
728x90
๋ฐ˜์‘ํ˜•

์ด์ œ๋Š” ์ด๋Ÿด ํ•„์š”๋„ ์—†์„ ๊ฒƒ ๊ฐ™์€๋ฐ ๊ทธ๋•Œ ๊ทธ ์‹œ์ ˆ์—” ์™œ ์ด๋Ÿฐ ๊ฑฐ์— ์˜ˆ๋ฏผํ–ˆ๋Š”์ง€ ใ…‹ใ…‹ใ…‹

 

๋„คํŠธ์›Œํฌ ํ”„๋กœ๊ทธ๋žจ ์ œ์ž‘ํ•  ๋•Œ,

#include <winsock2.h>๋ฅผ ์ด์šฉํ•ด์„œ

y = htons(x); ๋‚˜

y = htonl(x);๋ฅผ

์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์ €๊ฒŒ ์–ผ๋งˆ๋‚˜ ๋Š๋ฆฐ ๊ฑธ๊นŒ? ํ•œ ๋ฒˆ ์ƒ๊ฐํ•ด๋ดค๋‹ค.

/* Using Winsock2 normaly */
#include <iostream>
#include <winsock2.h>
using namespace std;

int main()
{
    unsigned short x = 0x3399;
    unsigned short y;

    unsigned int a = 0xff0102ff;
    unsigned int b;

    cout << "using winsock2" << hex << endl;

    for(int i=0;i<1000000000;i++)
    y = htons(x);
    cout << "x = " << x << "y = " << y << endl;

    for(int i=0;i<1000000000;i++)
    b = htonl(a);
    cout << "a = " << a << "b = " << b << endl;

    return 0;
}

๊ทธ๋ž˜์„œ ์ผ๋‹จ ํ…Œ์ŠคํŠธ๋กœ asm ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•ด์„œ htons์™€ htonl์„ ๊ตฌํ˜„ํ–ˆ๋‹ค.

asm ์ฝ”๋“œ๋Š” ์ธํ„ฐ๋„ท์—์„œ ๊ฒ€์ƒ‰ํ•˜๋ฉด ibm์ด๋ž‘ joinc๋ž‘ ๋ธ”๋กœ๊ทธ๋ž‘ ์—ฌ๋Ÿฌ๊ณณ์—์„œ ๋‚˜ํƒ€๋‚œ๋‹ค.

C๋กœ ๋œ ๋งˆ์Šคํ‚น ์ฒ˜๋ฆฌํ•ด์„œ ์‰ฌํ”„ํ„ฐ ์—ฐ์‚ฐํ•˜๋Š” ๊ฒƒ๋„ ์žˆ๋˜๋ฐ ๊ทธ๊ฑด ์†๋„๊ฐ€ ๋Š๋ฆด๊ฑฐ๊ณ ...

 

ํ•˜๋“œ์ฝ”๋“œ ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๋Š” ๋ฌด์ง€ ์ฐจ์ด๊ฐ€ ๋‚ฌ๋‹ค.

๊ฒฐ๊ณผ๋Š” ASM์ฝ”๋“œ์˜ ์Šน๋ฆฌ! (๋‹น์—ฐํ•œ๊ฑด๊ฐ€... ์ปดํŒŒ์ผ๋œ ํฌ๊ธฐ๋„ 84๋ฐ”์ดํŠธ ์ ๋‹ค.)

๋ฌผ๋ก  ์ด๋Ÿฐ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๊ฑด ๋‚ด๊ฐ€ ์“ฐ๋Š” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ MingW๋ผ์„œ ์ผ ์ˆ˜๋„ ์žˆ๋‹ค.

Visual C์—์„œ ์ปดํŒŒ์ผํ•˜๋ฉด ์ตœ์ ํ™”๋˜์„œ ์†๋„์ฐจ์ด๊ฐ€ ์•ˆ๋‚˜๊ฑฐ๋‚˜ ๋” ๋น ๋ฅผ ์ˆ˜๋„ ์žˆ๋‹ค. (ํ™•์ธ๋ถˆ๊ฐ€)

๊ตฌ๋ถ„ 1์–ตํšŒ ๊ณ„์‚ฐ  10์–ตํšŒ ๊ณ„์‚ฐ 
ASM์ฝ”๋“œ 0.687s 6.761s 
winsock2 1.374s 13.070s 

 

ํ•˜์ง€๋งŒ ํ•จ์ˆ˜ํ˜•ํƒœ๋กœ ๋ฐ”๊พธ๋‹ˆ๊นŒ๋ณ„๋กœ ์ฐจ์ด๊ฐ€ ๋‚˜์ง€ ์•Š์•˜๋‹ค.

๊ทธ๋ž˜์„œ ํ•จ์ˆ˜ ํ˜•ํƒœ๋กœ ์จ๋ณผ๋ ค๊ณ ...ํ–ˆ๋”๋‹ˆ ์ฐจ์ด๊ฐ€ ๋‚˜์ง€ ์•Š์•˜๋‹ค.

๋ผ๋Š” ์–˜๊ธฐ๋Š” winsock2๋„ asm ์ฝ”๋“œ๋ž‘ ๊ฐ™์€ ๊ฑฐ๊ฒ ์ง€.

๊ตฌ๋ถ„ 1์–ตํšŒ ๊ณ„์‚ฐ  10์–ตํšŒ ๊ณ„์‚ฐ 
ASM์ฝ”๋“œ 1.325s 13.491s
winsock2 1.374s 13.179s

 

#define๋ฌธ ์‚ฌ์šฉํ•ด์„œ ์žฌ์ •์˜ํ•œ fastHtons & fastHtonl

๊ฒฐ๊ตญ์€ #define์œผ๋กœ, ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์†Œ์Šค ์ค‘๊ฐ„์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋‹ˆ๊นŒ ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด ์—†์–ด์ง€๊ณ  ์†๋„๊ฐ€ ๋นจ๋ผ์ง„๋‹ค.

๋Œ€์‹  ์šฉ๋Ÿ‰์ด fastHtonX์˜ ์‚ฌ์šฉ ํšŸ์ˆ˜ ๋งŒํผ ๋Š˜์–ด๋‚œ๋‹ค. ๊ทธ๋ž˜๋ดค์ž ๋ช‡๋ฒˆ ์•ˆ ์“ฐ์ž”๋ƒ...ใ…‹

๊ตฌ๋ถ„ 1์–ตํšŒ ๊ณ„์‚ฐ  10์–ตํšŒ ๊ณ„์‚ฐ 
ASM์ฝ”๋“œ 0.687s 6.761s 
winsock2 1.374s 13.070s 

 

๋น ๋ฅธ ๋ฐ”์ดํŠธ ์˜ค๋” ์†Œ์Šค์ฝ”๋“œ

FastByteOrder.cc

/* fast byte order */
#include <iostream>
using namespace std;

#define fastNtohl fastHtonl

#define fastNtohs fastHtons

#define fastHtons(in, out) \
     asm ("movw %1, %%ax;"\
             "xchg %%ah, %%al;"\
             "movw %%ax, %0;"\
            :"=r"(out)\
            :"r"(in)\
            :"%ax","%ah","%al");

#define fastHtonl(in, out) \
     asm ("movl %1, %%eax;"\
             "bswap %%eax;"\
             "movl %%eax, %0;"\
            :"=r"(out)\
            :"r"(in)\
            :"%eax");

int main()
{
    unsigned short x = 0x3399;
    unsigned short y;

    unsigned int a = 0xff0102ff;
    unsigned int b;

    cout << "fast byte order" << hex << endl;

    for(int i=0;i<1000000000;i++)
    fastHtons(x, y);
    cout << "x = " << x << "y = " << y << endl;

    for(int i=0;i<1000000000;i++)
    fastHtonl(a, b);
    cout << "a = " << a << "b = " << b << endl;

    return 0;
}

 

์œ„ ๋‘ ์†Œ์Šค์ฝ”๋“œ์˜ ์‹คํ–‰ ์†๋„ ์ฐจ์ด ๋น„๊ต

์™ผ์ชฝ : Winsock2.h : 13.429s

์˜ค๋ฅธ์ชฝ : Fast Byte Order : 6.745s

๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์€ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ๋‚œ ์•ž์œผ๋กœ ์ด๊ฑธ ์“ธ๊ฑฐ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ฝ”๋”ฉ ๋ฐฉ์‹๋„ ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ์ค„์ด๊ณ , ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์— ๊ณฑํ•˜๊ธฐ๋„ ์ค„์ด๊ณ , ์™„์ „ ํ•˜๋“œ์ฝ”๋”ฉ์œผ๋กœ ใ…‹ใ…‹ใ…‹

๋ผ๊ณ  ์ƒ๊ฐํ•ด๋ดค์ง€๋งŒ, ๊ทธ๊ฑด ์ฝ”๋“œ๊ฐ€ 100์ค„ ์ดํ•˜์ผ ๋•Œ๋‚˜... ๊ฒฐ๊ตญ์€ ๊ฐ์ฒด ์„ค๊ณ„ ๋ฐฉ์‹์œผ๋กœ ๋งŒ๋“ค๊ฒŒ ๋˜๊ฒ ์ง€...

๊ฒŒ๋‹ค๊ฐ€ #define macro์˜ ๋‹จ์ ์ธ... ์—ฌ๋Ÿฌ ์ค„๋กœ ์ž‘์„ฑํ•  ๊ฒฝ์šฐ ๋„์ €ํžˆ ๋””๋ฒ„๊น… ํ•  ์ˆ˜ ์—†๋Š” ์‚ฌํƒœ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๊ทธ๊ฑธ๋กœ ์‚ฌ๋ง์ด๋‹ค. ใ…‹ใ…‹ใ…‹ (๋ฌผ๋ก  ํ•œ์ค„๋กœ ๋ฐ”๊พธ๋ฉด ํ•ด๊ฒฐ ๋จ-_ -;)

 

ํ˜น์‹œ ๋‚ด๊ฐ€ ์˜คํ•ดํ•œ ๊ฑด๊ฐ€ ์‹ถ์–ด์„œ ํ•œ๋ฒˆ ๋” ํ™•์ธํ•ด๋ด„.

๋””๋ฒ„๊น… ๋ชจ๋“œ๋ž‘ ๋ฆด๋ฆฌ์ฆˆ ๋ชจ๋“œ๊ฐ€ ์žˆ๋”๋ผ? ๊ทธ๋ž˜์„œ ๋‹ค์‹œ ํ…Œ์ŠคํŠธ ํ•ด๋ดค๋‹ค. ใ…Žใ…Žใ…Ž

1์ฐจ๋Š” ์ „๋ถ€ ์ฒซ ์ปดํŒŒ์ผ ํ›„ ์‹คํ–‰, 2์ฐจ๋Š” 1์ฐจ ์ข…๋ฃŒ ํ›„ ์‹คํ–‰, 3์ฐจ๋Š” 2์ฐจ ์ข…๋ฃŒ ํ›„ ์‹คํ–‰.

byte order by (asm)

๊ตฌ๋ถ„ Debug๋ชจ๋“œ  Release๋ชจ๋“œ
ํฌ๊ธฐ 604,188B 276,480B
1์ฐจ 8.796s 2.546s
2์ฐจ 8.750s 2.500s
3์ฐจ 8.750s 2.484s

byte order by (masking & shift)

๊ตฌ๋ถ„ Debug๋ชจ๋“œ  Release๋ชจ๋“œ
ํฌ๊ธฐ 1,322,981B 276,480B
1์ฐจ 9.656s 3.953s
2์ฐจ 9.609s 3.906s
3์ฐจ 9.593s 3.906s

byte order by (winsock2)

๊ตฌ๋ถ„ Debug๋ชจ๋“œ  Release๋ชจ๋“œ
ํฌ๊ธฐ 604,188B 276,480B
1์ฐจ 13.296s 8.984s
2์ฐจ 13.046s 8.968s
3์ฐจ 13.062s 8.953s

byte order by (asm function)

๊ตฌ๋ถ„ Debug๋ชจ๋“œ  Release๋ชจ๋“œ
ํฌ๊ธฐ 604,254B 276,480B
1์ฐจ 13.296s 6.421s
2์ฐจ 13.250s 6.375s
3์ฐจ 13.250s 6.375s

 

์˜์™ธ๋กœ ๋งˆ์Šคํ‚น & ์‰ฌํ”„ํŠธ๋„ ๋น ๋ฅด๊ตฌ๋‚˜! ๊ทธ๋ฆฌ๊ณ  ํ•จ์ˆ˜๋กœ ๋ฐ”๊ฟ”์„œ ์“ฐ๋ฉด winsock2๋ž‘ ๋น„์Šทํ•œ๋ฐ ์กฐ๊ธˆ ๋น ๋ฅด๊ธด ํ•˜์ง€๋งŒ 10์–ต๋ฒˆ ํ…Œ์ŠคํŠธ๋‹ˆ๊นŒ ์˜๋ฏธ๊ฐ€ ์žˆ๋‚˜ ์‹ถ๊ธฐ๋„?

728x90
๋ฐ˜์‘ํ˜•