Capturing a time in milliseconds

前端 未结 7 1113
一生所求
一生所求 2020-11-30 07:11

The following piece of code is used to print the time in the logs:

#define PRINTTIME() struct tm  * tmptime;
time_t     tmpGetTime;
time(&tmpGetTime);
tm         


        
7条回答
  •  暖寄归人
    2020-11-30 07:31

    If you don't want to use any OS-specific code, you can use the ACE package which supplies the ACE_OS::gettimeofday function for most standard operating systems. For example:

    ACE_Time_Value startTime = ACE_OS::gettimeofday();
    
    do_something();
    
    ACE_Time_Value endTime = ACE_OS::gettimeofday();
    
    cout << "Elapsed time: " << (endTime.sec() - startTime.sec()) << " seconds and " << double(endTime.usec() - startTime.usec()) / 1000 << " milliseconds." << endl;
    

    This code will work regardless of your OS (as long as ACE supports this OS).

提交回复
热议问题