Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/klib/blob/master/test/kvec_test.cc # klib [Ported from github.com/attractivechaos/klib] added block arena Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `klib / test / kvec_test.cc` 69 lines ยท 1679 bytes Raw Blame History 1 #include 2 #include 3 #include 4 #include 5 #include "kvec.h" 6 7 int main() 8 { 9 int M = 10, N = `20000000`, i, j; 10 clock_t t; 11 t = clock(); 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; 15 free(array); 16 } 17 printf("C array, preallocated: %.3f sec\n", 18 (float)(clock() - t) / CLOCKS_PER_SEC); 19 t = clock(); 20 for (i = 0; i < M; ++i) { 21 int *array = 0, max = 0; 22 for (j = 0; j < N; ++j) { 23 if (j == max) { 24 max = !max? 1 : max << 1; 25 array = (int*)realloc(array, sizeof(int)*max); 26 } 27 array[j] = j; 28 } 29 free(array); 30 } 31 printf("C array, dynamic: %.3f sec\n", 32 (float)(clock() - t) / CLOCKS_PER_SEC); 33 t = clock(); 34 for (i = 0; i < M; ++i) { 35 kvec_t(int) array; 36 kv_init(array); 37 kv_resize(int, array, N); 38 for (j = 0; j < N; ++j) kv_a(int, array, j) = j; 39 kv_destroy(array); 40 } 41 printf("C vector, dynamic(kv_a): %.3f sec\n", 42 (float)(clock() - t) / CLOCKS_PER_SEC); 43 t = clock(); 44 for (i = 0; i < M; ++i) { 45 kvec_t(int) array; 46 kv_init(array); 47 for (j = 0; j < N; ++j) 48 kv_push(int, array, j); 49 kv_destroy(array); 50 } 51 printf("C vector, dynamic(kv_push): %.3f sec\n", 52 (float)(clock() - t) / CLOCKS_PER_SEC); 53 t = clock(); 54 for (i = 0; i < M; ++i) { 55 std::vector array; 56 array.reserve(N); 57 for (j = 0; j < N; ++j) array[j] = j; 58 } 59 printf("C++ vector, preallocated: %.3f sec\n", 60 (float)(clock() - t) / CLOCKS_PER_SEC); 61 t = clock(); 62 for (i = 0; i < M; ++i) { 63 std::vector array; 64 for (j = 0; j < N; ++j) array.push_back(j); 65 } 66 printf("C++ vector, dynamic: %.3f sec\n", 67 (float)(clock() - t) / CLOCKS_PER_SEC); 68 return 0; 69 }