這隻程式分別使用迴圈和calloc的方法來對陣列初始化
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int n = 1024 * 1024 * 128 * sizeof(char); double rTime; clock_t clk0 , clk1; clk0 = clock(); char *pc = (char *)malloc(n); int i; for(i = 0 ; i < n ; i++) pc[i] = 0; clk1 = clock(); rTime = (double)(clk1 - clk0) / (double)CLOCKS_PER_SEC; printf("iterative clearing time: %f\n" , rTime); free(pc); clk0 = clock(); pc = (char *)calloc(n , sizeof(char)); clk1 = clock(); rTime = (double)(clk1 - clk0) / (double)CLOCKS_PER_SEC; printf("calloc time: %f\n" , rTime); free(pc); getchar(); return 0; }
可以看到其結果可以差到千倍之多呢@@"
全站熱搜
留言列表