linux - Why isn't the elapsed user time measured with getrusage() close to exactly consistent? -


this c++ program gives variable results. variation large. call getrusage() once start time. call rand() 500000000 times in loop. call getrusage() again , output elapsed user , system time between 2 getrusage() calls. depending on includes, can understand why "system time" not consistent. expected "user time" time (main process) thread in running state. thought close consistent 1 run next. it's not.

#include <iostream> #include <exception> #include <cstdlib>  #include <sys/types.h> #include <sys/time.h> #include <sys/resource.h>  using std::cout;  // tm end time on intput, time different start end on output. void since(const struct timeval start, struct timeval &tm)   {     if (tm.tv_sec == start.tv_sec)       {         tm.tv_sec = 0;         tm.tv_usec -= start.tv_usec;       }     else       {         tm.tv_usec += 1000000 - start.tv_usec;         tm.tv_sec -= start.tv_sec;         if (tm.tv_usec >= 1000000)           {             tm.tv_usec -= 1000000;             ++tm.tv_sec;           }       }   }  void out_tm(const struct timeval &tm)   {     cout << "seconds: " << tm.tv_sec;     cout << "  useconds: " << tm.tv_usec;   }  void bail(const char *msg)   {     cout << msg << '\n';     std::terminate();   }  int main()   {     struct rusage usage;      if (getrusage(rusage_self, &usage))       bail("fail:  getrusage() call failed");      struct timeval user_tm = usage.ru_utime;     struct timeval sys_tm = usage.ru_stime;      (unsigned = 0; < 500000000; ++i)       std::rand();      if (getrusage(rusage_self, &usage))       bail("fail:  getrusage() call failed");      since(user_tm, usage.ru_utime);     user_tm = usage.ru_utime;      since(sys_tm, usage.ru_stime);     sys_tm = usage.ru_stime;      cout << "user time:  ";     out_tm(user_tm);      cout << "\nsystem time:  ";     out_tm(sys_tm);     cout << '\n';      return(0);   } 

gnu recomments following code measuring time difference.

there differences code might cause jumping in time, try out.

int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) {   /* perform carry later subtraction updating y. */   if (x->tv_usec < y->tv_usec) {     int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;     y->tv_usec -= 1000000 * nsec;     y->tv_sec += nsec;   }   if (x->tv_usec - y->tv_usec > 1000000) {     int nsec = (x->tv_usec - y->tv_usec) / 1000000;     y->tv_usec += 1000000 * nsec;     y->tv_sec -= nsec;   }    /* compute time remaining wait.      tv_usec positive. */   result->tv_sec = x->tv_sec - y->tv_sec;   result->tv_usec = x->tv_usec - y->tv_usec;    /* return 1 if result negative. */   return x->tv_sec < y->tv_sec; } 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -