ページ 11

経過時間

Posted: 2009年5月14日(木) 14:33
by POP
このプログラムの計算結果の経過時間を表示したいのですがどのようにすればいいですか?
C言語初めてまだ2ヶ月で知識はそれほど深くありません。
よろしくお願いします。

#include <stdio.h>
#define MAX 100000 // 素数チェックの最大値
#define PRIME 0 // 素数
#define NOT_PRIME 1 // 素数でない

main()
{


int number[MAX+1]; // 素数チェック配列
int i, j; // カウンタ変数
int count; // 表示個数のカウンタ
// 素数チェック配列の初期化
for(i = 1; i <= MAX; i++)
number = PRIME;
// エラトステネスのふるい
number[1] = NOT_PRIME;
for(i = 2; i <= MAX; i++) {
if(number == PRIME) {

for(j = 2 * i; j <= MAX; j += i)
number[j] = NOT_PRIME;
}

}
// 素数の表示
count = 0;
for(i = 1; i <= MAX; i++) {

if(number == PRIME) {
printf("%5d", i);
count++;
if(count % 10 == 0)
printf("\n");
}
}
printf("素数の数: %d\n", count);


exit(0);

}

Re:経過時間

Posted: 2009年5月14日(木) 15:21
by non
ここを参考にされたらどうでしょうか?
http://kzk9.net/column/time.html

Re:経過時間

Posted: 2009年5月14日(木) 15:23
by POP
#include <sys/time.h>が見つかりませんと出てまったくプログラムが動かなかったんですけどどうすればいいですか?

Re:経過時間

Posted: 2009年5月14日(木) 15:28
by Mist
まず、規約を読んで
・開発環境の明記
・実行環境(OS)の明記
・ソースの字下げ
をしましょう。

一応言っておくとnonさんのリンク先はLinuxですよ。

Re:経過時間

Posted: 2009年5月14日(木) 15:39
by non
clockならWindowsでも使えます。

Re:経過時間

Posted: 2009年5月14日(木) 16:00
by やそ
time.h
が置いてあるフォルダの位置はあってます?

Re:経過時間

Posted: 2009年5月14日(木) 18:32
by KEYONN_
グローバル変数int sec=0;とし、
void STimer(void)
{
sec=GetTickCount();
}
でタイマーをセットし、
int PutTimer(void)
{
return (GetTickCount()-sec)/1000;
}
で、STimer()からかかった秒数を計算できます。
Windows限定ですが、参考にしてください。