C + + time (NULL) to calculate the duration length

Source: Internet
Author: User
Tags time interval

Sometimes in practical applications, you calculate the length of time an event lasts, such as calculating typing speed. In the 1th timer section, I've used the clock function to give an example. The Clock () function can be accurate to the millisecond level. At the same time, we can use the Difftime () function, but it is only accurate to seconds. The function is defined as follows:

Double Difftime (time_t time1, time_t TIME0);

Although the time interval in seconds that the function returns is double, this does not mean that the time has the same precision as the double, which is determined by its arguments (time_t is measured in seconds). For example, the following procedure:

#include <time.h> #include <stdio.h> #include <stdlib.h> int main (void) {time_t start,end;      Start = time (NULL);      System ("pause");      End = Time (NULL);      printf ("The pause used%f seconds.\n", Difftime (End,start));      System ("pause"); return 0; }

&NBSP
runs as follows:
Press any key to continue ...
The pause used 2.000000 seconds.
Press any key to continue ...

It can be imagined that the pause time is not so fortuitous for exactly 2 seconds. In fact, you will replace the line with the "//<-" annotation on the above program with one line of code:

printf ("The pause used%f seconds./n", End-start);
The
runs the same result.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.