28

我正在使用不包括 boost::timer 的 boost 1.46,还有什么其他方法可以为我的函数计时。

我目前正在这样做:

time_t now = time(0);
<some stuff>
time_t after = time(0);

cout << after - now << endl; 

但它只是在几秒钟内给出答案,所以如果函数需要 < 1s,它会显示 0。

谢谢

4

5 回答 5

30

在 Linux 或 Windows 中:

#include <ctime>
#include <iostream>

int
main(int, const char**)
{
     std::clock_t    start;

     start = std::clock();
     // your test
     std::cout << "Time: " << (std::clock() - start) / (double)(CLOCKS_PER_SEC / 1000) << " ms" << std::endl;
     return 0;
}

祝你好运 ;)

于 2013-02-26T15:17:49.097 回答
27

使用std::chrono

#include <chrono>
#include <thread>
#include <iostream>

// There are other clocks, but this is usually the one you want.
// It corresponds to CLOCK_MONOTONIC at the syscall level.
using Clock = std::chrono::steady_clock;
using std::chrono::time_point;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using namespace std::literals::chrono_literals;
using std::this_thread::sleep_for;

int main()
{
    time_point<Clock> start = Clock::now();
    sleep_for(500ms);
    time_point<Clock> end = Clock::now();
    milliseconds diff = duration_cast<milliseconds>(end - start);
    std::cout << diff.count() << "ms" << std::endl;
}

std::chrono是 C++11,std::literals是 C++14(否则你需要milliseconds(500))。

于 2016-05-07T17:50:50.997 回答
9

原来在 boost 1.46 中有一个时间版本(只是在不同的位置)。感谢@jogojapan 指出。

可以这样做:

#include <boost/timer.hpp>

timer t;
<some stuff>
std::cout << t.elapsed() << std::endl;

或者像@Quentin Perez 指出的那样使用 std 库(我会按照最初的要求接受)

于 2013-02-26T15:25:19.250 回答
3

基于 Quentin Perez 的解决方案,您可以使用 std::function 和 lambda 将任意函数传递给时间。

#include <ctime>
#include <iostream>
#include <functional>

void timeit(std::function<void()> func) {
    std::clock_t start = std::clock();

    func();

    int ms = (std::clock() - start) / (double) (CLOCKS_PER_SEC / 1000);

    std::cout << "Finished in " << ms << "ms" << std::endl;
}

int main() {
    timeit([] {
        for (int i = 0; i < 10; ++i) {
            std::cout << "i = " << i << std::endl;
        } 
    });

    return 0;
}
于 2016-01-05T21:15:57.890 回答
2

您可以使用 long 来保存当前时间值作为起始值,然后将当前时间转换为 double。这是一些用作示例的代码段。

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
int main()
{

struct      _timeb tStruct;
double      thisTime;
bool        done = false;
long        startTime;

 struct _timeb
 {
 int   dstflag;   // holds a non-zero value if daylight saving time is in effect
 long  millitm;   // time in milliseconds since the last one-second hack
 long  time;      // time in seconds since 00:00:00 1/1/1970
 long  timezone;  // difference in minutes moving west from UTC

 };

  _ftime(&tStruct); // Get start time

thisTime = tStruct.time + (((double)(tStruct.millitm)) / 1000.0); // Convert to double
startTime = thisTime;                                             // Set the starting time (when the function begins)


while(!done)     // Start an eternal loop
    {
    system("cls");  // Clear the screen
    _ftime(&tStruct);    // Get the current time
    thisTime = tStruct.time + (((double)(tStruct.millitm)) / 1000.0); // Convert to double
    // Check for 5 second interval to print status to screen
    cout << thisTime-startTime; // Print it. 

    }
}
于 2013-02-26T15:26:52.047 回答