2014-07-24 8 views
2

내 코드를 더 빨리 만들려고 노력 중이며 If-else 및 If 또는 else가 많이 있습니다. 스위치 케이스가 5 개 이상인 경우 스위치 케이스가 더 빠르다는 것을 안다. 따라서 if-elseif-or은 얼마나 빨랐습니까?if else if if 또는 vs switch case

if (item.Datum.Substring(5, 5) == "06-20" || item.Datum.Substring(5, 5) == "06-21") 
{ 
    Something 
} 
else if item.Datum.Substring(5, 5) == "06-22" || item.Datum.Substring(5, 5) == "06-23") 
{ 
    Something 
} 

또는

if (item.Datum.Substring(5, 5) == "06-20") 
{ 
    Something 
} 
else if (item.Datum.Substring(5, 5) == "06-21") 
{ 
    Something 
} 
else if (item.Datum.Substring(5, 5) == "06-22") 
{ 
    Something 
} 
else if (item.Datum.Substring(5, 5) == "06-23") 
{ 
    Something 
} 

또는 난 그냥 스위치 케이스와 함께 갈 것인가? 어떤 경우에는

switch(item.Datum.Substring(5, 5)) 
{ 
    case "06-20", "06,21": 
     Something 
     break; 
    case "06-22", "06,23": 
     Something 
     break; 
} 
+1

http://stackoverflow.com/questions/395618/is-there-any-significant-difference-between-using-if-else-and-switch-case -in-c – DarkBee

+0

[어느 쪽이 더 빠릅니까?] (http://ericlippert.com/2012/12/17/performance-rant/) –

+6

다른 부분을 제외하고는 '하위 문자열'을 여러 번 호출하지 마십시오! –

답변

1

는 상응하는 스위치 문은 IF-제표의 경우 명령문 또는 체인보다 느립니다. 주파수 휴리스틱 스를 사용하면 많은 프로그램에서 if 문으로 빠른 경로를 최적화 할 수 있습니다.

는 질문이 튀어 나올 때이 링크는 두 개의 서로 다른 비교

http://www.dotnetperls.com/if-switch-performance

0

내 철학을 발견 할 것이다 참조 : 적은 당신이 더 나은, 물품.

왜 그러한 철학입니까? 한 마디 : 테스트 가능성.
줄을 추가 할 때마다 해당 동작이 테스트되었는지 확인해야합니다. 나는 특히 switch synthax를 좋아하지 않지만 라인 번호를 2로 나눌 때 받아 들인다.

0

내가 갈 것입니다 :

dayStr = item.Datum.Substring(5, 5)) 
day = 'split '-', ',' and convert to int' 
switch day: 
{ 
    case 20: // Fall through 
    case 21: 
     // Do something 

    case 22: 
    ... 
} 
관련 문제