strlen 따라잡기 in KLDP.ORG

컴퓨터 이야기/C++ 2011. 1. 13. 03:25

[옛날 쓴글 가져오기 - mmoz]

제가 간단히 아는 ㅇ명령어로만 해봤는데 결과가 아주 비참하군요!!

셀러론 1Ghz 입니다.

__declspec(naked) int mmojz_strlen(const char* s)
{
	__asm
	{
		push ebp
		mov ebp, esp
	}
	__asm
	{
		push ecx
		push esi
		mov ecx, -1
		mov esi, dword ptr[ebp+8];
		cld
l:
		lodsb
		inc ecx
		cmp al, 0
		jne l
		mov eax, ecx
		pop esi
		pop ecx
	}
	__asm
	{
		pop ebp
		ret
	}
}

int mmojz2_strlen(const char* ch) 
{ 
	int i=0; 
	while (*ch++) i++; 
	return i; 
} 

int main()
{	
	unsigned int t;


	t = timeGetTime();
	for(volatile unsigned int i=0; i<10000000; i++)
	{
		strlen("345349534583495834958589358938595");
	}
	printf("라이브러리 : %d\n", timeGetTime()-t);

	t = timeGetTime();
	for(volatile unsigned int i=0; i<10000000; i++)
	{
		mmojz_strlen("345349534583495834958589358938595");
	}
	printf("허접 어셈 : %d\n", timeGetTime()-t);

	t = timeGetTime();
	for(volatile unsigned int i=0; i<10000000; i++)
	{
		mmojz2_strlen("345349534583495834958589358938595");
	}
	printf("C언어 : %d\n", timeGetTime()-t);
	return 0;
}

debug 버전에서는
라이브러리 : 1065
허접 어셈 : 2658
C언어 : 6016

Release 버전에서는
라이브러리 : 89
허접 어셈 : 2625
C언어 : 941

비참...


philossh wrote:
int asmstrlen(const char* ch)
{
	__asm
	{
		mov edi, dword ptr [ebp+8];
		xor al, al;
		mov ecx, -1;
      repne scasb;
		xor eax, eax;
		mov eax, -2;
		sub eax, ecx;
	}
}

은 Release 버전에서는 작동하지 않습니다. (.NET 2003 입니다.)


http://kldp.org/node/56157

BackSpace :: back
BackSpace :: back

'컴퓨터 이야기 > C++' 카테고리의 다른 글

C++ Standard draft 07  (0) 2011.02.05
나머지 연산자의 동작.  (2) 2011.01.13
Queue  (0) 2011.01.08
Little Bishops  (0) 2011.01.08
순열  (0) 2011.01.07
: