블로그에서 strncpy와 memcpy 중 memcpy가 더 빠르다고 본 기억이 있어서 확인차원에서 구글 codesearch를 빌려 확인
-------------------------------------------------------------------
STRNCPY
-------------------------------------------------------------------
char *
strncpy(char *dst, const char *src, size_t n)
{
if (n != 0) {
char *d = dst;
const char *s = src;
do {
if ((*d++ = *s++) == 0) {
/* NUL pad the remaining n-1 bytes */
while (--n != 0)
*d++ = 0;
break;
}
} while (--n != 0);
}
return (dst);
}
-------------------------------------------------------------------
strcpy는 assign 연산후에 널인지를 체크하는 비교 연산이 한번더 수행되고
널체크이후 타켓에 남은 size n만큼 0으로 패딩한다.
-------------------------------------------------------------------
MEMCPY
-------------------------------------------------------------------
void *memcpy(void *_dst, const void *_src, unsigned len)
{
unsigned char *dst = _dst;
const unsigned char *src = _src;
while(len-- > 0) {
*dst++ = *src++;
}
return _dst;
}
-------------------------------------------------------------------
memcpy는 심플하게 len만큼 복사하고 끝...
정확히 size를 알고 있다면 strncpy보다는 memcpy가 속도상 약간의 이득이 있는것으로 보임..
'C언어' 카테고리의 다른 글
TCP/IP accept() Interrupted system call (0) | 2011.05.03 |
---|---|
Google CPU Profiler (0) | 2010.08.30 |
introduce to TC( Tokyo cabinet ) (0) | 2010.08.25 |
label xxx defined but not used (0) | 2010.05.18 |
bit counting (0) | 2010.04.28 |