質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Linux

Linuxは、Unixをベースにして開発されたオペレーティングシステムです。日本では「リナックス」と呼ばれています。 主にWebサーバやDNSサーバ、イントラネットなどのサーバ用OSとして利用されています。 上位500のスーパーコンピュータの90%以上はLinuxを使用しています。 携帯端末用のプラットフォームAndroidは、Linuxカーネル上に構築されています。

Q&A

解決済

3回答

2173閲覧

linux 処理時間の表示

monamona154

総合スコア13

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Linux

Linuxは、Unixをベースにして開発されたオペレーティングシステムです。日本では「リナックス」と呼ばれています。 主にWebサーバやDNSサーバ、イントラネットなどのサーバ用OSとして利用されています。 上位500のスーパーコンピュータの90%以上はLinuxを使用しています。 携帯端末用のプラットフォームAndroidは、Linuxカーネル上に構築されています。

0グッド

0クリップ

投稿2016/07/06 07:11

編集2016/07/06 07:58

C言語でLinuxを使っています。メモリを確保したりするプログラムなのですが、以下のプログラムを修正して 、5秒間で何回の入れ替えを行えるかを計測できるようにしてもらいたいです。初心者なもので説明不足ですいません。参考にしたらいいサイトなどでも、何でも良いので手助けをお願いしたいです。
お願いします

#include <stdlib.h> #define VALUE_SIZE (1 * 1024 * 1024) struct array_data { int value[VALUE_SIZE]; }; struct list_data { struct list_data* next; int value[VALUE_SIZE]; }; void dump_array(struct array_data* data, int num) { int i; for (i = 0; i < num; ++i) { printf("%d\n", data[i].value[0]); } } void swap_array(struct array_data* array, int n) { struct array_data tmp; tmp = array[n]; array[n] = array[n + 1]; array[n + 1] = tmp; } void eval_array(int num) { struct array_data *array; int n, i; // init if ((array = malloc(sizeof(struct array_data) * num)) == NULL) { perror("malloc"); exit(1); } for (i = 0; i < num; ++i) { array[i].value[0] = i; } //dump_array(array, num); // swap n = (rand() % (num - 2)) + 1; swap_array(array, n); dump_array(array, num); free(array); } void dump_list(struct list_data* head) { struct list_data *ld; ld = head; while (ld != NULL) { printf("%d\n", ld->value[0]); ld = ld->next; } } void swap_list(struct list_data* head, int n) { } void eval_list(int num) { struct list_data *head, *ld; int n, i; // init if ((head = malloc(sizeof(struct list_data) * num)) == NULL) { perror("malloc"); exit(1); } ld = head; for (i = 0; i < num - 1; ++i) { ld->next = ld + 1; ld->value[0] = i; ld = ld->next; } ld->next = NULL; ld->value[0] = num - 1; //dump_list(head); // swap n = (rand() % (num - 2)) + 1; swap_list(head, n); dump_list(head); free(head); } int main(int argc, char *argv[]) { eval_array(10); //eval_list(10); exit(0); }

実行結果 $./koj.c
0
1
2
3
4
5
6
7
9
8

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

moonphase

2016/07/06 07:37

コードが見辛いので```で囲ってください
guest

回答3

0

https://teratail.com/questions/39894 のほうではベストアンサーありがとうございます

投稿2016/07/06 08:04

matobaa

総合スコア2493

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

swap_arrayを何回呼べるかということで良いのでしょうか。
やっつけですが、こんな感じでしょうか。

c

1#include <time.h> 2#include <sys/time.h> 34 // swap 5 long cnt = 0; 6 clock_t et = clock() + 5000000; 7 while(et >= clock()){ 8 n = (rand() % (num - 2)) + 1; 9 swap_array(array, n); 10 cnt++; 11 } 12 printf("cnt=%ld\n",cnt); 13

修正版全コード

c

1#include <stdlib.h> 2#include <time.h> 3#include <sys/time.h> 4 5#define VALUE_SIZE (1 * 1024 * 1024) 6 7struct array_data { 8 int value[VALUE_SIZE]; 9}; 10 11struct list_data { 12 struct list_data* next; 13 int value[VALUE_SIZE]; 14}; 15 16void dump_array(struct array_data* data, int num) { 17 int i; 18 19 for (i = 0; i < num; ++i) { 20 printf("%d\n", data[i].value[0]); 21 } 22} 23 24void swap_array(struct array_data* array, int n) { 25 struct array_data tmp; 26 27 tmp = array[n]; 28 array[n] = array[n + 1]; 29 array[n + 1] = tmp; 30} 31 32void eval_array(int num) { 33 struct array_data *array; 34 int n, i; 35 36 // init 37 if ((array = malloc(sizeof(struct array_data) * num)) == NULL) { 38 perror("malloc"); 39 exit(1); 40 } 41 42 for (i = 0; i < num; ++i) { 43 array[i].value[0] = i; 44 } 45 //dump_array(array, num); 46 47 // swap 48 long cnt = 0; 49 clock_t et = clock() + 5000000; 50 while(et >= clock()){ 51 n = (rand() % (num - 2)) + 1; 52 swap_array(array, n); 53 cnt++; 54 } 55 printf("cnt=%ld\n",cnt); 56 dump_array(array, num); 57 58 free(array); 59} 60 61void dump_list(struct list_data* head) { 62 struct list_data *ld; 63 64 ld = head; 65 while (ld != NULL) { 66 printf("%d\n", ld->value[0]); 67 ld = ld->next; 68 } 69} 70 71void swap_list(struct list_data* head, int n) { 72 73 } 74 75 76 77void eval_list(int num) { 78 struct list_data *head, *ld; 79 int n, i; 80 81 // init 82 if ((head = malloc(sizeof(struct list_data) * num)) == NULL) { 83 perror("malloc"); 84 exit(1); 85 } 86 87 ld = head; 88 for (i = 0; i < num - 1; ++i) { 89 ld->next = ld + 1; 90 ld->value[0] = i; 91 92 ld = ld->next; 93 } 94 ld->next = NULL; 95 ld->value[0] = num - 1; 96 //dump_list(head); 97 98 // swap 99 n = (rand() % (num - 2)) + 1; 100 swap_list(head, n); 101 dump_list(head); 102 103 free(head); 104} 105 106int main(int argc, char *argv[]) { 107 eval_array(10); 108 //eval_list(10); 109 110 exit(0); 111} 112

投稿2016/07/06 07:41

編集2016/07/06 14:02
ttyp03

総合スコア16996

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

monamona154

2016/07/06 13:10

すいません このプログラムを何処に配置したら動作するのかを教えてもらいたいです。 初診者ですいません。以下のようなエラーが出てしまいます tetst.c In function ‘eval_array’: test.c:102:5: error: ‘main’ is normally a non-static function [-Werror=main] int main(int argc, char *argv[]) { ^ test.c:107:1: error: expected declaration or statement at end of input } ^ cc1: all warnings being treated as errors <builtin>: recipe for target 'test.o' failed make: *** [test.o] Error 1
ttyp03

2016/07/06 14:03

元のコードに追加しただけなんですけどねぇ。 わかりづらかったでしょうか。 全コードを追加しておきましたので参考に。
monamona154

2016/07/06 17:45

実行した場合以下のエラーが出るのですが、これ自体は実行不可なのでしょうか? test.c: In function ‘dump_array’: test.c:20:5: error: implicit declaration of function ‘printf’ [-Werror=implicit-function-declaration] printf("%d\n", data[i].value[0]); ^ test.c:20:5: error: incompatible implicit declaration of built-in function ‘printf’ [-Werror] test.c:20:5: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’ test.c: In function ‘eval_array’: test.c:38:5: error: implicit declaration of function ‘perror’ [-Werror=implicit-function-declaration] perror("malloc"); ^ test.c:55:3: error: incompatible implicit declaration of built-in function ‘printf’ [-Werror] printf("cnt=%ld\n",cnt); ^ test.c:55:3: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’ test.c: In function ‘dump_list’: test.c:66:5: error: incompatible implicit declaration of built-in function ‘printf’ [-Werror] printf("%d\n", ld->value[0]); ^ test.c:66:5: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’ cc1: all warnings being treated as errors <builtin>: recipe for target 'test.o' failed
monamona154

2016/07/06 17:48

すいません 解決しました。ありがとうございます
guest

0

1.入れ替え開始時にtime()関数で、入れ替えを始めた時間を取得:beginTime
2.入れ替え直後にtime()関数で、今の時間を取得:currentTime
3.difftime()関数で、beginTimeからcurrentTimeまでの秒数を求める
4.その差が5秒以上なら、その時点の入れ替え回数を出力

投稿2016/07/06 07:23

moonphase

総合スコア6621

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

monamona154

2016/07/06 07:30

すいません 初心者なもので具体的な書き方が理解できてないので、サイトなどや何処に書くかなどもわかれば教えてもらいたいです。本当にすいません
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問