ページ 11

#include <sys/time.h>について

Posted: 2009年2月06日(金) 10:04
by JT
<pre>
#include <stdafx.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

double gettimeofday_sec()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec * 1e-6;
}

int main()
{
double t1, t2;

t1 = gettimeofday_sec();
/* 処理. */
t2 = gettimeofday_sec();
printf("%f\n", t2 - t1);

return 0;
}
</pre>
今、マイクロ秒の実装をしていて秒単位まで出せたんですがマイクロ秒の実装がうまく行きません。
エラーは#include <sys/time.h>が開けませんと出ます。

環境 VC++2005

Re:#include <sys/time.h>について

Posted: 2009年2月06日(金) 10:12
by TOMONORI
(C:\Program Files)\Microsoft Visual Studio 8\VC\include\sys
の中にtime.hは入っていますか?

Re:#include <sys/time.h>について

Posted: 2009年2月06日(金) 10:12
by Mist
sys/time.hはLinux系環境で使用できるヘッダです。
VCでは使えません。

#preタグの前後は半角でないと効果がありません

Re:#include <sys/time.h>について

Posted: 2009年2月06日(金) 10:16
by JT
Mistさんありがとうございます><

Re:#include <sys/time.h>について

Posted: 2009年2月06日(金) 10:21
by JT
timeGetTime関数を調べてみます

Re:#include <sys/time.h>について

Posted: 2009年2月06日(金) 10:29
by Mist
マイクロ秒を計測したいんでしょ?
timeGetTimeはミリ秒しか返ってこないですよ。

http://msdn.microsoft.com/ja-jp/library/cc428795.aspx

バグさんが言っておられた以下の関数を調べられたほうがよいと思います。
SetPriorityClass();
QueryPerformanceFrequency();
QueryPerformanceCounter();

Re:#include <sys/time.h>について

Posted: 2009年2月06日(金) 10:52
by JT
logの強化をしたいだけなのですが手間取ってます^^ 関数を調べて頑張ります

Re:#include <sys/time.h>について

Posted: 2009年2月06日(金) 11:07
by JT
2009:02:06:11:06:30 と出すのはできるのですがでマイクロ秒は難しい!!スレッドよりも!、、、

Re:#include <sys/time.h>について

Posted: 2009年2月06日(金) 11:20
by バグ
ああ、そういうことですか…
それって、基準になるポイントが分からないと計算のしようがないですよね?
で、おそらく基準を取得する方法は無いんじゃないかと…(^_^;)

つまり、難しい云々ではなく、不可能ではないかと…

Re:#include <sys/time.h>について

Posted: 2009年2月06日(金) 11:23
by JT
バグさん、ありがとうございます。私も調べていくうちにlogでマイクロ秒を出すのは不可能だと感じてきましたが突破口を諦めず探します。でもスレッドを壊さずの実装は難しい!!なつかない猫を追いかけてる感覚です

Re:#include <sys/time.h>について

Posted: 2009年2月06日(金) 12:56
by toyo
ミリ秒までなら
#include <windows.h>
#include <stdio.h>

int main(void)
{
    SYSTEMTIME tm;
    GetLocalTime(&tm);
    printf("%hu/%hu/%hu %hu:%hu:%hu.%hu\n", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond, tm.wMilliseconds);
    return 0;
}

Re:#include <sys/time.h>について

Posted: 2009年2月06日(金) 13:16
by JT
toyo さんありがとうございます。やはりOSの関係で戻りが遅くどの関数でも精密には出ないですね

Re:#include <sys/time.h>について

Posted: 2009年2月09日(月) 15:02
by JT
皆さんありがとうございました!!