Windows 시스템에 있다면 당신은 QueryPerformanceCounter()를 호출하여 높은 성능 카운터를 사용할 수 있습니다
#include <windows.h>
#include <string>
#include <iostream>
int main()
{
LARGE_INTEGER li = {0}, li2 = {0};
QueryPerformanceFrequency(&li);
__int64 freq = li.QuadPart;
QueryPerformanceCounter(&li);
// run your app here...
QueryPerformanceCounter(&li2);
__int64 ticks = li2.QuadPart-li.QuadPart;
cout << "Reference Implementation Ran In " << ticks << " ticks" << " (" << format_elapsed((double)ticks/(double)freq) << ")" << endl;
return 0;
}
은 ... 그냥 보너스로, 여기에 (경과 시간을 변환하는 기능입니다 설명 문자열 초, 부동 소수점) :
std::string format_elapsed(double d)
{
char buf[256] = {0};
if(d < 0.00000001)
{
// show in ps with 4 digits
sprintf(buf, "%0.4f ps", d * 1000000000000.0);
}
else if(d < 0.00001)
{
// show in ns
sprintf(buf, "%0.0f ns", d * 1000000000.0);
}
else if(d < 0.001)
{
// show in us
sprintf(buf, "%0.0f us", d * 1000000.0);
}
else if(d < 0.1)
{
// show in ms
sprintf(buf, "%0.0f ms", d * 1000.0);
}
else if(d <= 60.0)
{
// show in seconds
sprintf(buf, "%0.2f s", d);
}
else if(d < 3600.0)
{
// show in min:sec
sprintf(buf, "%01.0f:%02.2f", floor(d/60.0), fmod(d,60.0));
}
// show in h:min:sec
else
sprintf(buf, "%01.0f:%02.0f:%02.2f", floor(d/3600.0), floor(fmod(d,3600.0)/60.0), fmod(d,60.0));
return buf;
}
운영체제? –
"응용 프로그램을 실행하는 데 걸리는 실시간을 얻을 방법이 없습니다."이게 무슨 뜻입니까? 당신이 얻을 수없는이 '진짜'시간은 무엇입니까? 경과 시간은 나에게 진짜처럼 보인다. –
실시간은 프로그램에서 코드를 처리하는 데 걸리는 실제 시간입니다. 실시간이 아닌 OS는 프로세스를 관리하고 우선 순위 등에 따라 어떤 프로세스가 실행될 것인지를 전환합니다. 응용 프로그램이 실행되는 데 걸리는 전체 시간은 일반적으로 응용 프로그램이 실제로 처리하는 시간보다 큽니다. 프로세스와 응용 프로그램 사이에서 OS를 전환 할 때 약간의 대기 상태가 있기 때문입니다. –