2012-10-02 3 views
0

내가 좋아하는 뭔가 생각 :C++ : 시간 (크로노없이) 금액에 대한 알고리즘을 실행

// Runs algorithm() n times during an amount of seconds. 
void run(int seconds) { 
    clock_t start = clock(); 
    clock_t current = start; 
    while(double (current - start)/CLOCKS_PER_SEC <= seconds) { 
    algorithm(); 
    current = clock(); 
    } 
} 

나는 프로세스가 절전 모드로 전환되는시기 회계 시간을 피하기 위해 time() 이상 clock()를 선택했다. chrono을 사용하지 않고 이것을 달성하는 더 좋은 방법이 있는지 알고 싶습니다.

+0

'QueryPerformanceCounter'와 같은 더 정밀도가 높은 플랫폼 별 도구를 사용하거나 [Boost 's Timers] (http://www.boost.org/doc/libs/1_51_0/libs/timer/doc/)로 실험 할 수 있습니다. index.html). 기본 구현이 더 나은지 아닌지는 알 수 없습니다. – chris

+0

휴대용 솔루션이나 플랫폼 별 솔루션을 원하십니까? –

+0

왜'std :: chrono'를 피하고 싶습니까? – bames53

답변

2

STLSoft는 UNIX 및 Windows 플랫폼에서 작동하는 performance_counter을 가지고 있습니다. 라이브러리는 헤더로만 제공되며 간단한 포함 만하면됩니다.

#include <platformstl/performance/performance_counter.hpp> 

void run(int seconds) { 
    platformstl::performance_counter pc; 
    platformstl::performance_counter::interval_type current = 0; 
    pc.start(); 
    while ((current/1000) <= seconds){ 
    algorithm(); 
    current += pc.stop_get_milliseconds_and_restart(); 
    } 
} 

자신 만의 힌트를 찾을 수도 있습니다.