最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

Function of Caculating elapsed time

工作和技术 crifan 1726浏览 0评论

Next: , Previous: Time Basics, Up: Date and Time


21.2 Elapsed Time

One way to represent an elapsed time is with a simple arithmetic data type, as with the following function to compute the elapsed time between two calendar times. This function is declared in time.h.

— Function: double difftime (time_t time1, time_t time0)

The difftime function returns the number of seconds of elapsed time between calendar time time1 and calendar time time0, as a value of type double. The difference ignores leap seconds unless leap second support is enabled.

In the GNU system, you can simply subtract time_t values. But on other systems, the time_t data type might use some other encoding where subtraction doesn’t work directly.

The GNU C library provides two data types specifically for representing an elapsed time. They are used by various GNU C library functions, and you can use them for your own purposes too. They’re exactly the same except that one has a resolution in microseconds, and the other, newer one, is in nanoseconds.

— Data Type: struct timeval

The struct timeval structure represents an elapsed time. It is declared in sys/time.h and has the following members:

long int tv_sec
This represents the number of whole seconds of elapsed time.
long int tv_usec
This is the rest of the elapsed time (a fraction of a second), represented as the number of microseconds. It is always less than one million.
— Data Type: struct timespec

The struct timespec structure represents an elapsed time. It is declared in time.h and has the following members:

long int tv_sec
This represents the number of whole seconds of elapsed time.
long int tv_nsec
This is the rest of the elapsed time (a fraction of a second), represented as the number of nanoseconds. It is always less than one billion.

It is often necessary to subtract two values of type struct timeval or struct timespec. Here is the best way to do this. It works even on some peculiar operating systems where the tv_sec member has an unsigned type.

/* <span class="roman"><font face="Times New Roman">Subtract the `struct timeval' values X and Y,</font></span>
<span class="roman"><font face="Times New Roman">storing the result in RESULT.</font></span>
<span class="roman"><font face="Times New Roman">Return 1 if the difference is negative, otherwise 0.</font></span>  */
     
     int
     timeval_subtract (result, x, y)
          struct timeval *result, *x, *y;
     {
       /* <span class="roman"><font face="Times New Roman">Perform the carry for the later subtraction by updating </font></span><var>y</var><span class="roman"><font face="Times New Roman">.</font></span> */
       if (x-&gt;tv_usec &lt; y-&gt;tv_usec) {
         int nsec = (y-&gt;tv_usec - x-&gt;tv_usec) / 1000000 + 1;
         y-&gt;tv_usec -= 1000000 * nsec;
         y-&gt;tv_sec += nsec;
       }
       if (x-&gt;tv_usec - y-&gt;tv_usec &gt; 1000000) {
         int nsec = (x-&gt;tv_usec - y-&gt;tv_usec) / 1000000;
         y-&gt;tv_usec += 1000000 * nsec;
         y-&gt;tv_sec -= nsec;
       }
     
       /* <span class="roman"><font face="Times New Roman">Compute the time remaining to wait.</font></span>
<code>tv_usec</code><span class="roman"><font face="Times New Roman"> is certainly positive.</font></span> */
       result-&gt;tv_sec = x-&gt;tv_sec - y-&gt;tv_sec;
       result-&gt;tv_usec = x-&gt;tv_usec - y-&gt;tv_usec;
     
       /* <span class="roman"><font face="Times New Roman">Return 1 if result is negative.</font></span> */
       return x-&gt;tv_sec &lt; y-&gt;tv_sec;
     }

Common functions that use struct timeval are gettimeofday and settimeofday.

There are no GNU C library functions specifically oriented toward dealing with elapsed times, but the calendar time, processor time, and alarm and sleeping functions have a lot to do with them.

转载请注明:在路上 » Function of Caculating elapsed time

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
79 queries in 0.268 seconds, using 22.11MB memory