CC++言語のタイミング関数
2451 ワード
1.中関数clock()は、タイプclock_を返します.t,精度,ミリ秒レベル
#include
#include
#include
void test()
{
int i = 0;
int j = 0;
double a = 0;
while (i++ < 1000000)
while (j++ < 1000000)
{
a = sqrt(2.0);
}
}
int main(void)
{
clock_t start, finish;
double duration = 0.0;
start = clock();
test();
finish = clock();
duration = (double)(finish - start); // ms
duration = (double)(finish - start) / CLOCKS_PER_SEC; // s, //#define CLOCKS_PER_SEC 1000
printf("%f seconds
", duration);
return 0;
}
2. :QueryPerformanceCounter , , , , , 。
#include
#include
#include
#include
void test()
{
int i = 0;
int j = 0;
double a = 0;
while (i++ < 1000000)
while (j++ < 1000000)
{
a = sqrt(2.0);
}
}
int main(void)
{
LARGE_INTEGER start;
LARGE_INTEGER end;
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
test();
QueryPerformanceCounter(&end);
printf("user time : %.10f seconds
", (double)(end.QuadPart - start.QuadPart) / (double)freq.QuadPart);
return 0;
}
3. time(&t), ,
: , 1970 1 1 00:00:00 , t , 。
#include
#include
#include
#include
#include
int main()
{
time_t t;
time(&t);
printf("Today's date and time: %s",ctime(&t));
time_t first, second;
first=time(NULL);
Sleep(2000);
second=time(NULL);
printf("The difference is: %f seconds",difftime(second,first));
getch();
return 0;
}