C++ Keeping track of how many seconds has passed since start of program

前端 未结 5 1212
渐次进展
渐次进展 2021-02-05 15:47

I am writing a program that will be used on a Solaris machine. I need a way of keeping track of how many seconds has passed since the start of the program. I\'m talking very sim

5条回答
  •  太阳男子
    2021-02-05 15:58

    Use std::chrono.

    #include 
    #include 
    
    int main(int argc, char *argv[])
    {
       auto start_time = std::chrono::high_resolution_clock::now();
       auto current_time = std::chrono::high_resolution_clock::now();
    
       std::cout << "Program has been running for " << std::chrono::duration_cast(current_time - start_time).count() << " seconds" << std::endl;
    
       return 0;
    }
    

    If you only need a resolution of seconds, then std::steady_clock should be sufficient.

提交回复
热议问题