9 int M
= 10, N
= 20000000, i
, j
;
12 for (i
= 0; i
< M
; ++i
) {
13 int *array
= (int*)malloc(N
* sizeof(int));
14 for (j
= 0; j
< N
; ++j
) array
[j
] = j
;
17 printf("C array, preallocated: %.3f sec\n",
18 (float)(clock() - t
) / CLOCKS_PER_SEC
);
20 for (i
= 0; i
< M
; ++i
) {
21 int *array
= 0, max
= 0;
22 for (j
= 0; j
< N
; ++j
) {
24 max
= !max
? 1 : max
<< 1;
25 array
= (int*)realloc(array
, sizeof(int)*max
);
31 printf("C array, dynamic: %.3f sec\n",
32 (float)(clock() - t
) / CLOCKS_PER_SEC
);
34 for (i
= 0; i
< M
; ++i
) {
37 kv_resize(int, array
, N
);
38 for (j
= 0; j
< N
; ++j
) kv_a(int, array
, j
) = j
;
41 printf("C vector, dynamic(kv_a): %.3f sec\n",
42 (float)(clock() - t
) / CLOCKS_PER_SEC
);
44 for (i
= 0; i
< M
; ++i
) {
47 for (j
= 0; j
< N
; ++j
)
48 kv_push(int, array
, j
);
51 printf("C vector, dynamic(kv_push): %.3f sec\n",
52 (float)(clock() - t
) / CLOCKS_PER_SEC
);
54 for (i
= 0; i
< M
; ++i
) {
55 std::vector
<int> array
;
57 for (j
= 0; j
< N
; ++j
) array
[j
] = j
;
59 printf("C++ vector, preallocated: %.3f sec\n",
60 (float)(clock() - t
) / CLOCKS_PER_SEC
);
62 for (i
= 0; i
< M
; ++i
) {
63 std::vector
<int> array
;
64 for (j
= 0; j
< N
; ++j
) array
.push_back(j
);
66 printf("C++ vector, dynamic: %.3f sec\n",
67 (float)(clock() - t
) / CLOCKS_PER_SEC
);