繁体   English   中英

如何在没有boost :: timer的情况下以毫秒为单位计时

[英]How to time a function in milliseconds without boost::timer

我正在使用boost 1.46,它不包括boost :: timer,还有什么方法可以计算我的功能。

我目前正在这样做:

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

cout << after - now << endl; 

但它只是在几秒钟内给出答案,所以如果函数小于1,则显示0。

谢谢

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

祝好运 ;)

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

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

它可以这样做:

#include <boost/timer.hpp>

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

或者使用std libs作为@Quentin Perez指出(我会接受最初的问题)

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

您可以使用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. 

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM