Easily measure elapsed time

前端 未结 26 2156
栀梦
栀梦 2020-11-22 04:13

I am trying to use time() to measure various points of my program.

What I don\'t understand is why the values in the before and after are the same? I understand thi

26条回答
  •  情深已故
    2020-11-22 05:11

    Matlab flavored!

    tic starts a stopwatch timer to measure performance. The function records the internal time at execution of the tic command. Display the elapsed time with the toc function.

    #include 
    #include 
    #include 
    using namespace std;
    
    clock_t START_TIMER;
    
    clock_t tic()
    {
        return START_TIMER = clock();
    }
    
    void toc(clock_t start = START_TIMER)
    {
        cout
            << "Elapsed time: "
            << (clock() - start) / (double)CLOCKS_PER_SEC << "s"
            << endl;
    }
    
    int main()
    {
        tic();
        this_thread::sleep_for(2s);
        toc();
    
        return 0;
    }
    

提交回复
热议问题