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

前端 未结 5 1207
渐次进展
渐次进展 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 16:05

    #include 
    #include 
    #include 
    using namespace std;
    void wait ( int seconds );
    int main ()
    {
      time_t start, end;
      double diff;
      time (&start); //useful call
      for (int i=0;i<10;i++) //this loop is useless, just to pass some time.
      {
      printf ("%s\n", ctime(&start));
      wait(1);
      }
      time (&end);//useful call
    
      diff = difftime(end,start);//this will give you time spent between those two calls.
      printf("difference in seconds=%f",diff); //convert secs as u like
      system("pause");
      return 0;
    }
    void wait ( int seconds )
    {
      clock_t endwait;
      endwait = clock () + seconds * CLOCKS_PER_SEC ;
      while (clock() < endwait) {}
    }
    

    this should work fine on solaris/unix also, just remove win refs

提交回复
热议问题