为什么malloc+memset比calloc慢?
calloc
malloc
calloc
malloc
calloc
malloc
+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
memset
bzero(buf[i],BLOCK_SIZE)
我的问题是:malloc
+memset
calloc
calloc
largeQ
森林海
相关分类