728x90

기존 로직 : strlen을 이용한 빈 문자열 체크

// 파일 경로 유효성 검사 확인 함수
bool FileExistA( const char* InPath )
{
    if ( strlen( InPath ) == 0 ) return false;

    bool bResult = false;
    /*
        경로 확인 함수 내용...
        bResult  = ...
    */
    return bResult;
}

 

개선 로직 : strcmp를 이용한 빈 문자열 체크

// 파일 경로 유효성 검사 확인 함수
bool FileExistB( const char* InPath )
{
    if ( strcmp( InPath, "\0") == 0 ) return false;

    bool bResult = false;
    /*
        경로 확인 함수 내용...
        bResult  = ...
    */
    return bResult;
}

 

성능 확인 : 200,000,000 회 진행 시 약 2배 차이

int main()
{
    time_t startTime;
    time_t endTime;

    time( &startTime );
    for ( int count = 0; count < 200000000; ++count )
    {
        FileExistA( "FileExistTest/Test/Test/ItemIcon" );
        FileExistA( "" );
    }
    time( &endTime );
    double diffTimeA = difftime( endTime, startTime );

    time( &startTime );
    for ( int count = 0; count < 200000000; ++count )
    {
        FileExistB( "FileExistTest/Test/Test/ItemIcon" );
        FileExistB( "" );
    }
    time( &endTime );
    double diffTimeB = difftime( endTime, startTime );
}

결과

strlen을 이용한 함수의 경우 6초, strcmp를 이용한 함수의 경우 3초의 결과가 나왔다.

strlen를 사용한 함수의 경우 문자열을 순회한 결과로 빈 문자열을 체크하지만,

strcmp를 사용한 함수의 경우 null 문자와만 비교해 빠르게 빈 문자열 체크가 가능했다.

 

UE4 게임 프로젝트에 사용된 소스 중 일부를 빼내서 작성한 내용입니다.

실제 소스는 TCHAR* 문자열을 사용해 빈 문자열을 체크하고 있었으며,

1차 개선은 strcmp를 이용한 리팩토링

2차 개선은 FString으로 형변환 해주는 소스에서 FString::IsEmpty()를 이용한 리팩토링으로 진행.

 

이런 리팩토링이 모여 1 프레임이라도 올라갔으면 좋겠다...

 

https://en.cppreference.com/w/cpp/string/byte/strlen

https://en.cppreference.com/w/cpp/string/byte/strcmp

반응형

+ Recent posts