为什么malloc+memset比calloc慢?
callocmalloccallocmalloc
callocmalloc+memset
#include<stdio.h>#include<stdlib.h>#define BLOCK_SIZE 1024*1024*256int main(){
int i=0;
char *buf[10];
while(i<10)
{
buf[i] = (char*)calloc(1,BLOCK_SIZE);
i++;
}}time ./a.out **real 0m0.287s** user 0m0.095s sys 0m0.192s
#include<stdio.h>#include<stdlib.h>#include<string.h>#define BLOCK_SIZE 1024*1024*256int main(){
int i=0;
char *buf[10];
while(i<10)
{
buf[i] = (char*)malloc(BLOCK_SIZE);
memset(buf[i],'\0',BLOCK_SIZE);
i++;
}}time ./a.out **real 0m2.693s** user 0m0.973s sys 0m1.721s
memsetbzero(buf[i],BLOCK_SIZE)
我的问题是:malloc+memsetcalloccalloc
慕尼黑8549860
largeQ
森林海
随时随地看视频慕课网APP
相关分类