時間をテストしたい関数があります: function()
Cでこれを行う方法がわかりません。これが私がやりたいことの擬似コードです:
int main()
{
int startTime = getTime();
function();
int endTime = getTime();
print endTime - startTime;
}
これはCでどのように行われますか?
残念ながら、ANSI Cを使用してこれを行う方法はありません。ただし、gettimeofday
POSIX関数を使用してこれを行うことができます。
#include <sys/time.h>
struct timeval tv;
struct timeval start_tv;
gettimeofday(&start_tv, NULL);
// call your function here
double elapsed = 0.0;
gettimeofday(&tv, NULL);
elapsed = (tv.tv_sec - start_tv.tv_sec) +
(tv.tv_usec - start_tv.tv_usec) / 1000000.0;
または、プログラム全体の実行時間を必要とする場合はtime ./your_program
、コマンドラインで実行することもできます。
最後に、Windowsを使用している場合は、このtimeGetTime
関数を使用できます。
このコードはfunction()
100万回実行され、各関数呼び出しの平均実行時間を出力します。
#include <time.h>
#include <stdlib.h>
int main(int argc, const char ** argv)
{
time_t start, end;
start = time(NULL);
for (int i = 0; i < 1000000; ++i)
function();
end = time(NULL);
printf("%f\n", (end - start) / 1000000.0);
return 0;
}
#include <stdio.h>
#include <time.h>
int main()
{
clock_t start, finish;
start = clock();
....
finish = clock();
printf("Time: %d", ((double) (finish - start)) / CLOCKS_PER_SEC);
return 0;
}
clock()は、プロセスの開始以降のクロックティック数を取得します。したがって、終了から開始を減算し、CLOCKS_PER_SECで除算すると、実行時間が秒単位で表示されます。(詳細については、マニュアルを参照してください。
私はいつもこのクラスを使用しています(申し訳ありませんが、C ++です)。何年も前にどこかで見つけました。作者はわかりませんが、投稿しても大丈夫だと思います。
使用法:
Timer hello;
hello.start();
function();
hello.stop();
std::cout << "Total time in ms: " << hola.getElapsedTimeInMilliSec() << std::endl;
クラス自体:
#include "timer.hh"
#include <stdlib.h>
///////////////////////////////////////////////////////////////////////////////
// constructor
///////////////////////////////////////////////////////////////////////////////
Timer::Timer()
{
#ifdef WIN32
QueryPerformanceFrequency(&frequency);
startCount.QuadPart = 0;
endCount.QuadPart = 0;
#else
startCount.tv_sec = startCount.tv_usec = 0;
endCount.tv_sec = endCount.tv_usec = 0;
#endif
stopped = 0;
startTimeInMicroSec = 0;
endTimeInMicroSec = 0;
}
///////////////////////////////////////////////////////////////////////////////
// distructor
///////////////////////////////////////////////////////////////////////////////
Timer::~Timer()
{
}
///////////////////////////////////////////////////////////////////////////////
// start timer.
// startCount will be set at this point.
///////////////////////////////////////////////////////////////////////////////
void Timer::start()
{
stopped = 0; // reset stop flag
#ifdef WIN32
QueryPerformanceCounter(&startCount);
#else
gettimeofday(&startCount, NULL);
#endif
}
///////////////////////////////////////////////////////////////////////////////
// stop the timer.
// endCount will be set at this point.
///////////////////////////////////////////////////////////////////////////////
void Timer::stop()
{
stopped = 1; // set timer stopped flag
#ifdef WIN32
QueryPerformanceCounter(&endCount);
#else
gettimeofday(&endCount, NULL);
#endif
}
///////////////////////////////////////////////////////////////////////////////
// compute elapsed time in micro-second resolution.
// other getElapsedTime will call this first, then convert to correspond resolution.
///////////////////////////////////////////////////////////////////////////////
double Timer::getElapsedTimeInMicroSec()
{
#ifdef WIN32
if(!stopped)
QueryPerformanceCounter(&endCount);
startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart);
endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
#else
if(!stopped)
gettimeofday(&endCount, NULL);
startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
#endif
return endTimeInMicroSec - startTimeInMicroSec;
}
///////////////////////////////////////////////////////////////////////////////
// divide elapsedTimeInMicroSec by 1000
///////////////////////////////////////////////////////////////////////////////
double Timer::getElapsedTimeInMilliSec()
{
return this->getElapsedTimeInMicroSec() * 0.001;
}
///////////////////////////////////////////////////////////////////////////////
// divide elapsedTimeInMicroSec by 1000000
///////////////////////////////////////////////////////////////////////////////
double Timer::getElapsedTimeInSec()
{
return this->getElapsedTimeInMicroSec() * 0.000001;
}
///////////////////////////////////////////////////////////////////////////////
// same as getElapsedTimeInSec()
///////////////////////////////////////////////////////////////////////////////
double Timer::getElapsedTime()
{
return this->getElapsedTimeInSec();
}
GetTickCount windows api とAbout Timers (for Windows) を見てください。
gettimeofday
and/orclock
関数を調べます。もっと高精度のクロック/タイマーがあるかもしれませんが、これらはコードの優れた断面には十分なはずです。