/*******************************************************************
* 문자열이 있는지 검사할 때 흔히 if (문자열개체 != "")나
* if (문자열개체 != Stirng.Empty)를 쓰는데 이럴 경우 불필요한 오버헤드가 생기지만,
* Length 속성은 개체가 생성될때 설정되므로
* if (문자열개체.Length != 0 )로 해서 값만 비교해서 보다 빠르게 검사할 수 있습니다.
* ******************************************************************/
using System;
namespace StringEmptyVSStringLength
{
/// <summary>
/// Class1에 대한 요약 설명입니다.
/// </summary>
class Class1
{
/// <summary>
/// 해당 응용 프로그램의 주 진입점입니다.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 여기에 응용 프로그램을 시작하는 코드를 추가합니다.
//
string notEmptyString = "값이 있는 문자열";
// IL_000c: call bool [mscorlib]System.String::op_Inequality(string, string)
if (notEmptyString != String.Empty)
{
}
//IL_0014: callvirt instance int32 [mscorlib]System.String::get_Length()
if (notEmptyString.Length != 0)
{
}
}
}
}
IL 코드로는 1가지 명령어지만
"if (notEmptyString != String.Empty)"에서는 12가지 내부 개체의 메서드를 사용해 비교하려는 양쪽의 문자열을 정렬하고 서로 일일히 비교하지만,
i"f (notEmptyString.Length != 0)"에서는 단순이 변수에 값이 0이냐만 비교하기 때문에 속도가 빠를수 밖에 없죠.
실행해 시간을 검사하면 아래와 같은 차이를 보입니다. 단위는 (Microseconds)
742.4 if (notEmptyString != String.Empty)
0.7 if (notEmptyString.Length != 0)
- 출처 : KNUG (한국 닷넷 사용자 모임; Korea .NET User Group)
2005/07/11
값 없는 문자열은 Length로 검사로 속도향상
가장 많이 본 글
-
처리되지 않은 예외: System.Security.Authentication.AuthenticationException: SSPI를 호출하지 못했습니다. 내부 예외를 참조하십시오. ---> System.ComponentModel.Win32Exce...
-
다음 이유 때문에 원격 도메인 'outlook.com'(으)로 배달하는 동안 '52.96.111.82' 호스트로 메시지를 배달하지 못했습니다. An SMTP protocol error occurred. 오류를 일으킨 S...
-
public DateTime UnixTimeToDateTime ( double value ) { DateTime unixEpoch = new DateTime( 1970 , 1 , 1 , 0 , 0 , 0 , 0 ).ToLocalTime()...
댓글 없음:
댓글 쓰기