Easily measure elapsed time

前端 未结 26 2201
栀梦
栀梦 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:07

    #include 
    
    void f() {
      using namespace std;
      clock_t begin = clock();
    
      code_to_time();
    
      clock_t end = clock();
      double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
    }
    

    The time() function is only accurate to within a second, but there are CLOCKS_PER_SEC "clocks" within a second. This is an easy, portable measurement, even though it's over-simplified.

提交回复
热议问题