以下のプログラムを修正して
5秒間で何回の入れ替えを行えるかを計測できるようにしてもらいたいです
C言語
1#include <stdio.h> 2#include <stdlib.h> 3 4#define VALUE_SIZE (1 * 1024 * 1024) 5 6struct array_data { 7 int value[VALUE_SIZE]; 8}; 9 10struct list_data { 11 struct list_data* next; 12 int value[VALUE_SIZE]; 13}; 14 15void dump_array(struct array_data* data, int num) { 16 int i; 17 18 for (i = 0; i < num; ++i) { 19 printf("%d\n", data[i].value[0]); 20 } 21} 22 23void swap_array(struct array_data* array, int n) { 24 struct array_data tmp; 25 26 tmp = array[n]; 27 array[n] = array[n + 1]; 28 array[n + 1] = tmp; 29} 30 31void eval_array(int num) { 32 struct array_data *array; 33 int n, i; 34 35 // init 36 if ((array = malloc(sizeof(struct array_data) * num)) == NULL) { 37 perror("malloc"); 38 exit(1); 39 } 40 41 for (i = 0; i < num; ++i) { 42 array[i].value[0] = i; 43 } 44 //dump_array(array, num); 45 46 // swap 47 n = (rand() % (num - 2)) + 1; 48 swap_array(array, n); 49 dump_array(array, num); 50 51 free(array); 52} 53 54void dump_list(struct list_data* head) { 55 struct list_data *ld; 56 57 ld = head; 58 while (ld != NULL) { 59 printf("%d\n", ld->value[0]); 60 ld = ld->next; 61 } 62} 63 64void swap_list(struct list_data* head, int n) { 65 struct list_data *ld0, *ld1, *ld2; 66 int i; 67 68 if(n < 1){ 69 printf("n=%d\n",n); 70 return; 71 } 72 73 ld0 = head; 74 for(i = 0; i < n - 1; ++i) 75 ld0 = ld0->next; 76 ld1 = ld0->next; 77 ld2 = ld1->next; 78} 79 80 81void eval_list(int num) { 82 struct list_data *head, *ld; 83 int n, i; 84 85 // init 86 if ((head = malloc(sizeof(struct list_data) * num)) == NULL) { 87 perror("malloc"); 88 exit(1); 89 } 90 91 ld = head; 92 for (i = 0; i < num - 1; ++i) { 93 ld->next = ld + 1; 94 ld->value[0] = i; 95 96 ld = ld->next; 97 } 98 ld->next = NULL; 99 ld->value[0] = num - 1; 100 //dump_list(head); 101 102 // swap 103 n = (rand() % (num - 2)) + 1; 104 swap_list(head, n); 105 dump_list(head); 106 107 free(head); 108} 109 110int main(int argc, char *argv[]) { 111 eval_array(10); 112 //eval_list(10); 113 114 exit(0); 115} 116
計測して回数が出力されたら大丈夫だと思います。帰って実行してみます
回答1件
あなたの回答
tips
プレビュー