C++ C++;:在一定时间内运行算法(不使用chrono)
我在想这样的事情:C++ C++;:在一定时间内运行算法(不使用chrono),c++,timing,C++,Timing,我在想这样的事情: // 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()
// 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();
}
}
//在几秒钟内运行算法()n次。
无效运行(整数秒){
时钟启动=时钟();
时钟电流=启动;
而(双(当前-启动)/CLOCKS_PER__SECSTLSoft有一个可在UNIX和windows平台上运行的库。该库仅为标题库,只需要一个简单的include:
#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();
}
}
#包括
无效运行(整数秒){
平台STL::性能计数器pc;
platformstl::性能计数器::间隔类型当前=0;
pc.start();
而((当前/1000)您可以使用更高精度、特定于平台的工具,例如QueryPerformanceCounter
,或者尝试,尽管我不知道底层实现是否更好。您想要一个可移植的解决方案还是特定于平台的解决方案?为什么有人想要避免std::chrono
?@bames53 C++11不是t中的选项你在上述方法中有哪些需要“更好”方法的具体问题?