- 论坛徽章:
- 0
|
和乘法操作几乎没有关系.
实际情况是, cmalloc速度应该快一点.
我有程序, 大家可以实验速度.
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #define MAX 512 //give a value equal to your RAM
- #define LEN 1024*1024
- int main(int argc, char **argv)
- {
- int i;
- char *p;
- time_t t;
- t = time(0);
- printf("begin...\n");
- if(argv[1] && argv[1][0] == 'c')
- for ( i = 0; i < MAX; i++) {
- p = (char *)calloc(LEN,sizeof(char));
- if ( p == NULL ) {
- printf("calloc error!\n");
- exit(1);
- }
- free(p);
- }
- else
- for ( i = 0; i < MAX; i++) {
- p = (char *)malloc(LEN*sizeof(char));
- memset(p,0,LEN*sizeof(char));
- if ( p == NULL ) {
- printf("calloc error!\n");
- exit(1);
- }
- free(p);
- }
- t = time(0) - t;
- printf("%ld\n",t);
- exit(0);
- }
- 然后根据结果给出合理的解释.
复制代码 |
|