如何以毫秒为单位的功能,而不升压::计时器(How to time a function in mi

2019-07-20 14:16发布

我使用升压1.46不包括升压::计时器,还有什么其他办法能我一次我的功能。

我目前在做这样的:

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

cout << after - now << endl; 

但它只是给出了在秒的回答,因此,如果函数取<1秒显示为0。

谢谢

Answer 1:

在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;
}

祝好运 ;)



Answer 2:

使用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)



Answer 3:

原来存在的升压1.46(刚刚在不同的位置)时的版本。 由于@jogojapan指点出来。

这是可以做到这样的:

#include <boost/timer.hpp>

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

或可选择地使用std库作为@Quentin佩雷斯指出(我将接受什么最初问)



Answer 4:

在昆汀·佩雷斯的解决方案的基础上,你可以使用std ::功能和拉姆达传递一个任意功能的时间。

#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;
}


Answer 5:

您可以使用长以保存当前时间值作为初始值,然后将当前时间转换为双。 这里是一些代码片段作为例子来使用。

#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. 

    }
}


文章来源: How to time a function in milliseconds without boost::timer