関数実行時間テスト
1772 ワード
関数の実行時間をテストします.
使用方法:
//実行時間コードtimeval tvをテストする;gettimeofday(&tv, NULL); double cl = tv.tv_sec + (double)tv.tv_usec/1000000;
//テストコード
gettimeofday(&tv, NULL);
cl = (tv.tv_sec + (double)tv.tv_usec/1000000) - cl; printf(「実行時間:%0.3 f秒」,cl);
たとえば、反転文字コードをテストします.
#define WIN32 // VC WIN32 gettimeofday()
#include <time.h>
#ifdef WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif
#ifdef WIN32
int gettimeofday(struct timeval *tp, void *tzp)
{
time_t clock;
struct tm tm;
SYSTEMTIME wtm;
GetLocalTime(&wtm);
tm.tm_year = wtm.wYear - 1900;
tm.tm_mon = wtm.wMonth - 1;
tm.tm_mday = wtm.wDay;
tm.tm_hour = wtm.wHour;
tm.tm_min = wtm.wMinute;
tm.tm_sec = wtm.wSecond;
tm.tm_isdst = -1;
clock = mktime(&tm);
tp->tv_sec = clock;
tp->tv_usec = wtm.wMilliseconds * 1000;
return (0);
}
#endif /* WIN32 */
使用方法:
//実行時間コードtimeval tvをテストする;gettimeofday(&tv, NULL); double cl = tv.tv_sec + (double)tv.tv_usec/1000000;
//テストコード
gettimeofday(&tv, NULL);
cl = (tv.tv_sec + (double)tv.tv_usec/1000000) - cl; printf(「実行時間:%0.3 f秒」,cl);
たとえば、反転文字コードをテストします.
void Reverse(char *word) // C
{ // C++ Primer Plus forstr2.cpp -- reversing an array
char temp;
size_t i, j;
for (j = 0, i = strlen(word) - 1; j < i; --i, ++j) {
temp = word[i];
word[i] = word[j];
word[j] = temp;
}
}
int main()
{
using namespace std;
// 1KW , ,string C
//
timeval tv;
gettimeofday(&tv, NULL);
double cl = tv.tv_sec + (double)tv.tv_usec / 1000000;
char cs[] = "0123456789abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i != 10000001; i++)
Reverse(cs);
cout << cs << endl;
gettimeofday(&tv, NULL);
cl = (tv.tv_sec + (double)tv.tv_usec / 1000000) - cl;
printf("
: %0.3f
", cl);
return 0;
}