Timer function to provide time in nano seconds using C++

前端 未结 16 2291
野趣味
野趣味 2020-11-22 06:02

I wish to calculate the time it took for an API to return a value. The time taken for such an action is in the space of nano seconds. As the API is a C++ class/function, I a

16条回答
  •  误落风尘
    2020-11-22 06:34

    I am using the following to get the desired results:

    #include 
    #include 
    using namespace std;
    
    int main (int argc, char** argv)
    {
        // reset the clock
        timespec tS;
        tS.tv_sec = 0;
        tS.tv_nsec = 0;
        clock_settime(CLOCK_PROCESS_CPUTIME_ID, &tS);
        ...
        ... 
        ...
        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tS);
        cout << "Time taken is: " << tS.tv_sec << " " << tS.tv_nsec << endl;
    
        return 0;
    }
    

提交回复
热议问题