header-img
Info :
728x90

 

๊ฐœ๋ฐœํ•˜๋‹ค๋ณด๋ฉด ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š”๊ฒŒ ํ•„์ˆ˜์ ์ด์–ด์ง„๋‹ค.

์ด๋•Œ switch๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ๋‚˜์€์ง€ else if ์˜ ์‚ฌ์šฉ์ด ๋‚˜์€์ง€ ๊ณ ๋ฏผ์ด ๋  ๋•Œ๊ฐ€ ์žˆ๋‹ค.

 

๋ณดํ†ต ์ฝ”๋“œ ์ •๋ฆฌ๋Š” switch๊ฐ€ ํ•œ ๋ˆˆ์— ๋ณด๊ธฐ ์‰ฌ์šด ํŽธ์ด๊ณ ,

๊ฐœ์ธ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ์—๋Š” if~else ๋ฌธ์ด ์ฒ˜๋ฆฌํ•˜๊ธฐ๊ฐ€ ์‰ฌ์šด ๊ฒƒ ๊ฐ™๋‹ค.

 

๊ทธ๋ž˜๋„ ์ฝ”๋“œ ๊ฐ€์‹œ์„ฑ ๋ฐ ๊ฐœ๋ฐœ์ž ํŽธ์˜์„ฑ์„ ๊ณ ๋ คํ•˜์ง€ ์•Š์„ ์ˆ˜๋Š” ์—†์ง€๋งŒ...

์ œ์ผ ์ค‘์š”ํ•œ ๊ฑด ํ”„๋กœ๊ทธ๋žจ ์„ฑ๋Šฅ์ด๋ผ... ๋‘ ๊ฐ€์ง€์˜ ๋กœ์ง์ ์ธ ์ฐจ์ด๋ฅผ ์ •๋ฆฌํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.

 

 

switch๋ฌธ

๋ณ€์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ์ž…๋ ฅ ๋ฐ›์•„ ๋‘๊ณ , ๋ฏธ๋ฆฌ ์ •ํ•ด๋†“์€ ๊ฐ’๋“ค๊ณผ ์ผ์น˜ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜์—ฌ control flow๋ฅผ ์ฒ˜๋ฆฌ

 

if~else๋ฌธ

boolean์œผ๋กœ ๊ฒฐ๊ณผ ๊ฐ’์ด ๋‚˜์˜ค๋Š” ์กฐ๊ฑด๋ฌธ์˜ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ true, false ๋‘๊ฐ€์ง€ control flow๋ฅผ ๊ฐ€์ง„๋‹ค.

 

๊ทธ๋Ÿฌ๋ฏ€๋กœ . . . 
if~else ๋ฌธ์„ ๋ชจ๋‘ switch๋กœ ๋ณ€ํ™˜ ์‹œํ‚ค์ง€๋Š” ๋ชปํ•˜์ง€๋งŒ
switch ๋ฌธ์œผ๋กœ ์ž‘์„ฑ๋œ ๋ฌธ๋ฒ•์„ ๋ชจ๋‘ if~else ๋กœ ๋ณ€ํ™˜ ์‹œํ‚ค๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

๊ฒฐ๊ณผ์ ์œผ๋กœ ๋‘๊ฐœ๋ฅผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜๋„๋ก ๊ตฌํ˜„์€ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ

๊ตฌ์กฐ์ ์œผ๋กœ๋Š” ์–ด๋–จ๊นŒ?

 

 

else if ๊ตฌ๋ฌธ๊ณผ switch ๊ตฌ๋ฌธ์˜ logic์ ์ธ ์ฐจ์ด 

 

if else ๊ตฌ๋ฌธ

๊ฐ๊ฐ ์กฐ๊ฑด๋ฌธ์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ control flow๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.

N๊ฐœ์˜ if else ๋ฌธ์ด ์žˆ์œผ๋ฉด N๊ฐœ ์กฐ๊ฑด๋ฌธ์˜ ์ง„์œ„์—ฌ๋ถ€๋ฅผ ํŒ๋‹จ.

 

 

Switch ๊ตฌ๋ฌธ

JVM์—์„œ switch ๊ตฌ๋ฌธ์•ˆ์˜ case ๊ฐ’๋“ค์˜ ๋ถ„ํฌ์— ๋”ฐ๋ผ

๋‚ด๋ถ€์ ์œผ๋กœ ๊ฐ๊ฐ์˜ case์— ์ตœ์ ํ™”๋œ 2๊ฐœ์˜ ์ž๋ฐ” ๋ฐ”์ดํŠธ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋Š”๋ฐ...

๊ณตํ†ต์ ์œผ๋กœ ๋‘ ๊ฒฝ์šฐ ๋ชจ๋‘ hashtable์ด ์—ฐ์ƒ๋˜๋Š” ๊ตฌ์กฐ๋ฅผ ๋ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  case ๊ฐ’์— ๋”ฐ๋ผ ์ตœ์ ํ™” ๋ฃจํ‹ด์„ ์„ค์ •ํ•˜๊ฒŒ ๋œ๋‹ค.

- case๊ฐ’๋“ค์ด ์„œ๋กœ ํฐ ์ฐจ์ด์—†์ด ๋ถ™์–ด์žˆ๋Š” ๊ฒฝ์šฐ: TableSwitch ํ˜•์‹์˜ ์ปดํŒŒ์ผ ์ˆ˜ํ–‰
- case๊ฐ’๋“ค์ด ์„œ๋กœ ์ฐจ์ด๊ฐ€ ํฌ๊ฒŒ ๋‚  ๊ฒฝ์šฐ : LookupSwitch ํ˜•์‹์˜ ์ปดํŒŒ์ผ ์ˆ˜ํ–‰

 

TableSwitch ๋ฐฉ์‹

case๋กœ ์ฃผ์–ด์ง„ ๊ฐ’๊ณผ case๋“ค ์‚ฌ์ด์˜ ๊ฐ’๋“ค์— ํ•ด๋‹นํ•˜๋Š” case๊นŒ์ง€ ์ „๋ถ€ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฐ”์ดํŠธ์ฝ”๋“œ๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ์‹

HashTable ๊ณผ ์œ ์‚ฌํ•œ ๊ตฌ์กฐ : ์ž๋ฐ” ๋ฐ”์ดํŠธ ์ฝ”๋“œ์—์„œ lable๋งŒ์„ ์‚ฌ์šฉ

 

LookupSwitch ๋ฐฉ์‹

TableSwitch ๋ฐฉ์‹์˜ ์ฝ”๋“œ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ, ์‚ฌ์ด ๊ฐ’๋“ค์„ ํ•ด์‹œ ํ˜•ํƒœ๋กœ ๊ณ„์‚ฐํ•ด๋‘์ง€ ์•Š์Œ.

List ์™€ ์œ ์‚ฌํ•œ ๊ตฌ์กฐ : ์ž๋ฐ” ๋ฐ”์ดํŠธ ์ฝ”๋“œ์—์„œ key์™€ ํ•จ๊ป˜ label์„ ์‚ฌ์šฉ

 

์ตœ์ข…์ ์œผ๋กœ...

Switch ๊ตฌ๋ฌธ์€ item์˜ ๊ฐœ์ˆ˜(N)์— ๋”ฐ๋ผ O(lgN) (์ตœ์•…์˜ ๊ฒฝ์šฐ) ์— ์‹คํ–‰๋˜๊ณ 
if~else ๊ตฌ๋ฌธ์€ item์˜ ๊ฐœ์ˆ˜(N)์— ๋”ฐ๋ผ ์‹คํ–‰๋œ๋‹ค.. O(N)

 

 

๊ฒ€์ฆ

 

๋™์ผ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋Œ€์ƒ์œผ๋กœ for ๋ฌธ ์ค‘์ฒฉ ํ•˜์—ฌ ํ…Œ์ŠคํŠธ๋ฅผ ์‹œ์ผœ๋ณด์ž.

 

// in case: if~else ๊ตฌ๋ฌธ ์‚ฌ์šฉ

long a = System.currentTimeMillis();
int ret = -1;
 
for (int i=0; i<1000000000; ++i)
    for (int num=0; num<5; ++num)
        if (num == 0)      ret = 10;
        else if (num == 1) ret = 20;
        else if (num == 2) ret = 30;
        else if (num == 3) ret = 40;
        else if (num == 4) ret = 50;
        else               ret = 12;
long b = System.currentTimeMillis();
System.out.println(ret);
System.out.println(b-a+"ms");

 

50
8718ms
// in case: Switch ๊ตฌ๋ฌธ ์‚ฌ์šฉ

long a = System.currentTimeMillis();
int ret = -1;
 
for (int i=0; i<1000000000; ++i)
    for (int num=0; num<5; ++num)
        switch (num) {
        case 0: ret = 10; break;
        case 1: ret = 20; break;
        case 2: ret = 30; break;
        case 3: ret = 40; break;
        case 4: ret = 50; break;
        default: ret = 12; break;
        }
long b = System.currentTimeMillis();
System.out.println(ret);
System.out.println(b-a+"ms");

 

50
2820ms

 

๋ถ„๊ธฐ ๊ตฌ๋ฌธ์ด ์•ฝ 5๊ฐœ ์ •๋„์— ๋ถˆ๊ณผํ•œ๋ฐ์—๋„ switch๋ฌธ์ด 3~4๋ฐฐ ์ •๋„ ๋น ๋ฅธ ๊ฒƒ์„ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

๋ถ„๊ธฐ ๊ตฌ๋ฌธ์ด ๋Š˜์–ด๋‚ ์ˆ˜๋ก ์ฐจ์ด๋Š” ๋ฒŒ์–ด์งˆ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋œ๋‹ค.

 

 

์š”์•ฝ

์•ž์—์„œ ๋งํ–ˆ๋“ฏ์ด ๋ณดํ†ต ๊ตฌํ˜„ ์‹œ๊ฐ„์ด๋‚˜ ์ต์ˆ™ํ•˜๋‹ค๋Š” ์ด์œ ๋กœ 

'ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜ ๊ฐ’์„ ๋ฐ›์•„ ๊ทธ ๊ฐ’์„ ๋น„๊ตํ•ด control flow๋ฅผ ์ •ํ•˜๋Š” ์ฝ”๋“œ'๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ

if~else ๊ตฌ๋ฌธ์„ ๋งŽ์ด ์ฑ„ํƒํ•˜๊ฒŒ ๋œ๋‹ค.

 

ํ•˜์ง€๋งŒ ์†๋„ ๊ฐ™์€ ํผํฌ๋จผ์Šค ์ธก๋ฉด์—์„œ๋Š” switch ๋ฌธ์ด ์šฐ์„ธํ•˜๋ฏ€๋กœ...

์ฐธ๊ณ ํ•˜์—ฌ ๋ฒ„๋ฆ‡์„ ๋‹ค์‹œ ๋“ค์ด๋Š” ๊ฒƒ๋„ ์ข‹์„ํ…Œ๋‹ค.

 

+)

case๋ฌธ์˜ ๊ฒฝ์šฐ ์—ฐ์‚ฐ์„ ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ๊ทธ๊ฑธ๋กœ ๊ฒฐ๊ณผ ๊ฐ’์„ ๊ฐ€์ ธ๋‹ค ์ฃผ๋ฏ€๋กœ

"๋ฉ”๋ชจ๋ฆฌ" ์ธก๋ฉด์—์„œ ๋‹จ์ ์„ ๊ฐ€์ง€๊ธด ํ•œ๋‹ค.

 

 

๋ถ„๊ธฐ 3๊ฐœ๊นŒ์ง€๋Š” if~else๋ฌธ. ๊ทธ ์ด์ƒ์˜ ๋ถ„๊ธฐ๋Š” switch~case๋ฌธ.



 

 

 

728x90
๋”๋ณด๊ธฐ
FRONTEND/React