bubblesortとquicksortの性能比較に関数ポインタを導入
bubblesortとquicksortを関数ポインタで扱うようにした。
これがコードの抽象度を上げるということかな。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
#include <stdio.h> #include <stdlib.h> #include <time.h> // 時間経過ナノ秒計算 int calc_timelapse_nsec(struct timespec start, struct timespec end){ if(start.tv_sec == end.tv_sec){ // 秒が同じ return end.tv_nsec - start.tv_nsec; }else{ // 秒が異なる int diff_sec = end.tv_sec - start.tv_sec; int diff_nsec = diff_sec * 1000000000; return diff_nsec + (end.tv_nsec - start.tv_nsec); } } void dispArrayList(int *a, int length){ for(int i=0; i<length; i++){ printf("[%d] : %d\n", i, a[i]); } } void dispArrayLine(int *a, int length){ for(int i=0; i<length; i++){ printf("%d", a[i]); if(i == length - 1){ printf("\n"); }else{ printf(" "); } } } void resetArray(int *a, int length){ for(int i=0; i<length; i++){ a[i] = 0; } } void init(){ srand(time(NULL)); } double getRandomDouble(){ return rand() * 1.0 / RAND_MAX; } int getRandomIntMinMax(int min, int max){ if(max < min){ return max; }else{ return min + (int)(getRandomDouble() * (max - min + 1)); } } int getRandomInt(){ int min = 0; int max = 9; return getRandomIntMinMax(min, max); } void setRandomForArray(int *a, int length){ for(int i=0; i<length; i++){ a[i] = getRandomInt(); } } void swap(int *a, int l, int r){ int t = a[l]; a[l] = a[r]; a[r] = t; } void bubblesort(int *a, int length){ for(int i=0; i<length-1; i++){ for(int j=i; j<length; j++){ if(a[j] < a[i]){ swap(a, i, j); } } } } int getPivot(int a, int b, int c){ if(b < a){ if(a < c){ return a; }else{ return b < c ? b : c; } }else{ if(b < c){ return b; }else{ return a < c ? a : c; } } } void quicksort_core(int *a, int length, int left, int right){ if(right <= left){ return; } int l = left; int r = right; int p = getPivot(a[l], a[l+1], a[r]); while(1){ while(a[l] < p){ l++; } while(p < a[r]){ r--; } if(r <= l){ break; } swap(a, l, r); l++; r--; } quicksort_core(a, length, left, l-1); quicksort_core(a, length, r+1, right); } void quicksort(int *a, int length){ int left = 0; int right = length - 1; quicksort_core(a, length, left, right); } int calcTimeLapse_sort_nsec( void (*sort)(int*, int), int *a, int length ){ int b[length]; for(int i=0; i<length; i++){ b[i] = a[i]; } struct timespec ts_start, ts_end; timespec_get(&ts_start, TIME_UTC); sort(b, length); timespec_get(&ts_end, TIME_UTC); return calc_timelapse_nsec(ts_start, ts_end); } int calcTimeLapse_sort_average_nsec( void (*sort)(int*, int), int length, int times ){ int a[length]; int sum_nsec = 0; for(int i=0; i<times; i++){ setRandomForArray(a, length); sum_nsec += calcTimeLapse_sort_nsec(sort, a, length); } return sum_nsec / times; } int main(void){ /* 準備 */ init(); /* 宣言 */ int times = 10000; int length = 30; /* 処理 */ int timelapse_bubblesort_average_nsec = calcTimeLapse_sort_average_nsec(bubblesort, length, times); int timelapse_quicksort_average_nsec = calcTimeLapse_sort_average_nsec(quicksort, length, times); /* 出力 */ printf("timelapse_bubblesort_average_nsec : %d\n", timelapse_bubblesort_average_nsec); printf("timelapse_quicksort_average_nsec : %d\n", timelapse_quicksort_average_nsec); } |
1 2 |
timelapse_bubblesort_average_nsec : 1845 timelapse_quicksort_average_nsec : 1177 |