转自:
http://apps.hi.baidu.com/share/detail/38844734关于字符数组的初始化,在项目的压力测试中,发现性能明显下降,变怀疑在程序中的若干临时字符数组的初始化(使用bzero)身上。于是修改为首个字符置零的方式而非全部置零的方式初始化,响应得到明显的提升。原来在mp3检索的每一条结果都要进行bzero对临时数组初始化,每一个请求需要30次的bzero对临时数组的置零。于是想到了,在非必要的情况下,只对临时数组的第一个(或前几个)字符置零的初始化方式对比与使用bzero的话,能够明显提高性能。
在此之外,又想起另外两种对数组所有字节都置零的方式,顺便比较一下他们之间的性能,写个简单的程序如下:
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#define TIMEDIFF(s, e) (((e.tv_sec)-(s.tv_sec))*1000000 + (e.tv_usec) - (s.tv_usec))
int main()
{
struct timeval s, e;
char a[1024], b[1024*1024], c[1024*1024*4];
gettimeofday(&s, NULL);
bzero(a, sizeof(a));
gettimeofday(&e, NULL);
printf("bzero 1k: %d\n", TIMEDIFF(s, e));
gettimeofday(&s, NULL);
bzero(b, sizeof(b));
gettimeofday(&e, NULL);
printf("bzero 1m: %d\n", TIMEDIFF(s, e));
gettimeofday(&s, NULL);
bzero(c, sizeof(c));
gettimeofday(&e, NULL);
printf("bzero 4M: %d\n", TIMEDIFF(s, e));
gettimeofday(&s, NULL);
memset(a, 0, sizeof(a));
gettimeofday(&e, NULL);
printf("memset 1k: %d\n", TIMEDIFF(s, e));
gettimeofday(&s, NULL);
memset(b, 0, sizeof(b));
gettimeofday(&e, NULL);
printf("memset 1M: %d\n", TIMEDIFF(s, e));
gettimeofday(&s, NULL);
memset(c, 0, sizeof(c));
gettimeofday(&e, NULL);
printf("memset 4M: %d\n", TIMEDIFF(s, e));
gettimeofday(&s, NULL);
for(int i=0; i<sizeof(a); ++i)
a[i]=0;
gettimeofday(&e, NULL);
printf("for 1k: %d\n", TIMEDIFF(s, e));
gettimeofday(&s, NULL);
for(int i=0; i<sizeof(b); ++i)
b[i]=0;
gettimeofday(&e, NULL);
printf("for 1M: %d\n", TIMEDIFF(s, e));
gettimeofday(&s, NULL);
memset(c, 0, sizeof(c));
gettimeofday(&e, NULL);
printf("memset 4M: %d\n", TIMEDIFF(s, e));
gettimeofday(&s, NULL);
for(int i=0; i<sizeof(a); ++i)
a[i]=0;
gettimeofday(&e, NULL);
printf("for 1k: %d\n", TIMEDIFF(s, e));
gettimeofday(&s, NULL);
for(int i=0; i<sizeof(b); ++i)
b[i]=0;
gettimeofday(&e, NULL);
printf("for 1M: %d\n", TIMEDIFF(s, e));
gettimeofday(&s, NULL);
for(int i=0; i<sizeof(c); ++i)
c[i]=0;
gettimeofday(&e, NULL);
printf("for 4M: %d\n", TIMEDIFF(s, e));
}
运行的结果基本上是,在数组较小的情况下,bzero的效率比memset高;当数组超过一定大小之后,bzero的效率开始比memset低;数组越大,memset的性能优势越明显。而在数组较小的情况下,memset的性能甚至不如直接for循环对数组中的每一个字节置零的方法。
以下的运行结果的数值单位是微秒(gettimeofday的默认单位)。
第一次运行:
bzero 1k: 6
bzero 1m: 2168
bzero 4M: 9136
memset 1k: 11
memset 1M: 1303
memset 4M: 5483
for 1k: 12
for 1M: 4934
for 4M: 21313
再一次运行:
bzero 1k: 6
bzero 1m: 2160
bzero 4M: 9067
memset 1k: 17
memset 1M: 1257
memset 4M: 5115
for 1k: 11
for 1M: 4866
for 4M: 19201
此后,又写了个小程序,测试在堆上的数组中,bzero和memset的效率,发现两者差不多。可能由于,里面原来的数据就比较有规则,不管是否先对数组置一随机值。(malloc开辟字符数组空间时,会清零的。)
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
#include <time.h>
#define TIMEDIFF(s, e) (((e.tv_sec)-(s.tv_sec))*1000000 + (e.tv_usec) - (s.tv_usec))
int main()
{
srand(time(NULL));
char *array;
struct timeval s, e;
int tb, tm;
for(int i=1; i<1024*1024*1024; i*=2)
{
array=(char*)malloc(i);
memset(array, rand()%256, i);
gettimeofday(&s, NULL);
bzero(array, i);
gettimeofday(&e, NULL);
tb=TIMEDIFF(s, e);
free(array);
array=(char*)malloc(i);
memset(array, rand()%256, i);
gettimeofday(&s, NULL);
memset(array, 0, i);
gettimeofday(&e, NULL);
tm=TIMEDIFF(s, e);
free(array);
printf("array size: %d \tbzero time: %d \tmemset time: %d \tbzero>memset?: %d\n", i, tb, tm, (tb>tm));
}
}
运行结果:
array size: 1 bzero time: 28 memset time: 1 bzero>memset?: 1
array size: 2 bzero time: 1 memset time: 1 bzero>memset?: 0
array size: 4 bzero time: 1 memset time: 1 bzero>memset?: 0
array size: 8 bzero time: 1 memset time: 1 bzero>memset?: 0
array size: 16 bzero time: 1 memset time: 1 bzero>memset?: 0
array size: 32 bzero time: 1 memset time: 1 bzero>memset?: 0
array size: 64 bzero time: 1 memset time: 0 bzero>memset?: 1
array size: 128 bzero time: 1 memset time: 1 bzero>memset?: 0
array size: 256 bzero time: 1 memset time: 1 bzero>memset?: 0
array size: 512 bzero time: 1 memset time: 1 bzero>memset?: 0
array size: 1024 bzero time: 1 memset time: 1 bzero>memset?: 0
array size: 2048 bzero time: 1 memset time: 1 bzero>memset?: 0
array size: 4096 bzero time: 2 memset time: 2 bzero>memset?: 0
array size: 8192 bzero time: 2 memset time: 2 bzero>memset?: 0
array size: 16384 bzero time: 5 memset time: 6 bzero>memset?: 0
array size: 32768 bzero time: 9 memset time: 8 bzero>memset?: 1
array size: 65536 bzero time: 27 memset time: 24 bzero>memset?: 1
array size: 131072 bzero time: 81 memset time: 68 bzero>memset?: 1
array size: 262144 bzero time: 190 memset time: 169 bzero>memset?: 1
array size: 524288 bzero time: 447 memset time: 393 bzero>memset?: 1
array size: 1048576 bzero time: 996 memset time: 973 bzero>memset?: 1
array size: 2097152 bzero time: 2258 memset time: 2272 bzero>memset?: 0
array size: 4194304 bzero time: 4821 memset time: 4799 bzero>memset?: 1
array size: 8388608 bzero time: 9797 memset time: 9799 bzero>memset?: 0
array size: 16777216 bzero time: 19764 memset time: 19737 bzero>memset?: 1
array size: 33554432 bzero time: 39687 memset time: 39675 bzero>memset?: 1
array size: 67108864 bzero time: 79907 memset time: 79324 bzero>memset?: 1
array size: 134217728 bzero time: 158956 memset time: 158775 bzero>memset?: 1
array size: 268435456 bzero time: 318247 memset time: 318632 bzero>memset?: 0
array size: 536870912 bzero time: 638536 memset time: 638883 bzero>memset?: 0