一連の別のプログラムを実行する必要がある C のプログラムがあります。これらの時間のログを作成するには、これらの各プログラムの実行時間を取得する必要があります。
system() を使用して各プログラムを実行することを考えましたが、実行時間を取得する方法がわかりません。これを行う方法はありますか?
プログラムは「速い」ので、秒以上の精度が必要です。
あなたはそれをするために少なくとも4つの方法があります。
開始点:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ( void )
{
clock_t start = clock();
system("Test.exe");
printf ("%f\n seconds", ((double)clock() - start) / CLOCKS_PER_SEC);
return 0;
}
Windowsを使用していて、Window APIにアクセスできる場合は、次のものも使用GetTickCount()
できます。
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main ( void )
{
DWORD t1 = GetTickCount();
system("Test.exe");
DWORD t2 = GetTickCount();
printf ("%i\n milisecs", t2-t1);
return 0;
}
そして最高は
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main(void)
{
LARGE_INTEGER frequency;
LARGE_INTEGER start;
LARGE_INTEGER end;
double interval;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start);
system("calc.exe");
QueryPerformanceCounter(&end);
interval = (double) (end.QuadPart - start.QuadPart) / frequency.QuadPart;
printf("%f\n", interval);
return 0;
}
質問にはタグが付けられていますC
が、完全を期すために、機能を追加したいと思いC++11
ます。
int main()
{
auto t1 = std::chrono::high_resolution_clock::now();
system("calc.exe");
auto t2 = std::chrono::high_resolution_clock::now();
auto x = std::chrono::duration_cast<std::chrono::nanoseconds>(t2-t1).count();
cout << x << endl;
}
start = clock(); // get number of ticks before loop
/*
Your Program
*/
stop = clock(); // get number of ticks after loop
duration = ( double ) (stop - start ) / CLOCKS_PER_SEC;