Chinaunix首页 | 论坛 | 博客
  • 博客访问: 21246
  • 博文数量: 5
  • 博客积分: 251
  • 博客等级: 二等列兵
  • 技术积分: 70
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-27 10:46
文章分类

全部博文(5)

文章存档

2011年(4)

2010年(1)

我的朋友

分类: C/C++

2011-09-21 12:35:25

昨天在网上看到有人说strncpy的性能问题,大概解释如下:
strncpy(dest,src,n)
当n > strlen(src) 时,该函数会在后面补上n - strlen(src)个'\0',这个操作用一个for循环完成的,是个比较低效率的操作。
见man page:
NAME
       strcpy, strncpy - copy a string
SYNOPSIS
       #include
       char *strcpy(char *dest, const char *src);
       char *strncpy(char *dest, const char *src, size_t n);
DESCRIPTION
       The  strcpy()  function  copies  the  string  pointed to by src (including the terminating '\0' character) to the array
       pointed to by dest.  The strings may not overlap, and the destination string dest must be large enough to  receive  the
       copy.
       The  strncpy() function is similar, except that not more than n bytes of src are copied. Thus, if there is no null byte
       among the first n bytes of src, the result will not be null-terminated.
       In the case where the length of src is less than that of n, the remainder of dest will be padded with null bytes.
 (这一点也查看了glibc的代码,确实如此)
 
采用strlen+memcpy要比strncpy快很多,而且linux内核也是这样实现的。
 
抱着“纸上得来终觉浅,绝知此事要躬行”的态度,写了两个小程序测试了一下:
cat test1.c
#include
int main(){
 char src[] = "1234567890";
 char dest[1024];
 
 int len = 0;
 for(int i = 0; i < 10000000; ++i){
  memset(dest, 0, sizeof(dest));
  strncpy(dest, src, sizeof(dest));
 }
 return 0;
}
 
 
cat test2.c
#include
int main(){
 char src[] = "1234567890";
 char dest[1024];
 
 int len = 0;
 for(int i = 0; i < 10000000; ++i){
  memset(dest, 0, sizeof(dest));
  len  = strlen(src);
  len = sizeof(dest) - 1 > len? len: sizeof(dest) -1;
  memcpy(dest, src, len);
  dest[len] = '\0';
 }
 return 0;
}
 
gcc -std=c99 test1.c -o test1
gcc -std=c99 test2.c -o test2
time ./test1
real 0m10.712s
user 0m10.701s
sys 0m0.000s
time ./test2
real 0m1.287s
user 0m1.280s
sys 0m0.000s
没有使用任何优化选项,性能差别8倍左右
gcc -std=c99 -O3 test1.c -o test1
gcc -std=c99 -O3 test2.c -o test2
time ./test1
real 0m10.747s
user 0m10.733s
sys 0m0.000s
time ./test2
real 0m0.505s
user 0m0.504s
sys 0m0.000s
采用O3优化,性能差别居然达到20倍
 
结论:strncpy性能确实不够好,在对性能非常敏感的模块建议采用自己的strncpy(可以是strlen+memcpy)。
阅读(1545) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~