一直有人提出,meset和bzero的效率问题,认为在初始化小对象的时候,bzero比memset高效一点。今天写了个测试程序,如下:
这段测试代码,是分别对4字节整数,11字节的数组,8192字节的数组进行初始化,执行1000000次,求平均值。
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static inline unsigned long long rdtsc(void)
{
unsigned hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}
int main()
{
unsigned long long start , end;
unsigned long long diff;
unsigned long long memset_avg_diff1 , memset_avg_diff2 , memset_avg_diff3;
unsigned long long bzero_avg_diff1, bzero_avg_diff2 , bzero_avg_diff3;
int x , i;;
char buf[11];
char big_buf[8192];
memset_avg_diff1 = memset_avg_diff2 = memset_avg_diff3 = 0;
bzero_avg_diff1 = bzero_avg_diff2 = bzero_avg_diff3 &