C Language test program execution time __c language

Source: Internet
Author: User
Tags local time month name microsoft c

http://blog.csdn.net/asaasa66/article/details/6543929


Test program run Time-time.h

1. Timing

The timing function in C/s + + is clock (), and its associated data type is clock_t. In MSDN, the clock function is identified as follows:

clock_t clock (void);

This function returns the number of CPU clock timing units (clock tick) between the time the "start this program process" and the call to clock () function in the program), which is called the Wall Clock Time (Wal-clock) in MSDN. Where clock_t is the data type used to hold the time, in the Time.h file, we can find the definition of it:

#ifndef _clock_t_defined
typedef long CLOCK_T;
#define _clock_t_defined
#endif

Obviously, clock_t is a long shaping number. In the time.h file, a constant clocks_per_sec is also defined to indicate how many clock ticks will be in a second, defined as follows:

#define CLOCKS_PER_SEC ((clock_t) 1000)//clocks_per_sec for system customization

You can see that every 1 per thousand seconds (1 milliseconds), call the value returned by the clock () function plus 1. For the following example, you can use the formula clock ()/clocks_per_sec to calculate the elapsed time of a process itself:

void Elapsed_time ()
{
printf ("Elapsed time:%u secs./n", Clock ()/clocks_per_sec);
}

Of course, you can also use the clock function to calculate how long your machine runs a loop or how much time it takes to handle other events:

#include "stdio.h"
#include "Stdlib.h"
#include "time.h"

int main ()
{
Long i = 10000000L;
clock_t start, finish;
Double total_time;
/* Measure the duration of an event/
printf ("Time to does%ld empty loops is", i);
start = Clock ();
while (i--);
finish = Clock ();
Total_time = (double) (finish-start)/clocks_per_sec;
printf ("%f seconds/n", total_time);
return 0;

}

On the author's machine, the results of the operation are as follows:

10000000 empty loops is 0.03000 seconds

We see that the clock timer is 1 milliseconds long and the precision of the timer is 1 milliseconds, so we can make the timing more accurate by changing the definition of clocks_per_sec by defining it larger. By trying, you'll find that this is not going to work. The smallest unit of timekeeping in standard C/s is one millisecond.

2. Data structures related to date and time

The TM structure can be used to obtain the date and time in standard C + +, and the TM structure is defined in Time.h as follows:

#ifndef _tm_defined
struct TM {
int tm_sec; /* SEC – The value range is [0,59] * *
int tm_min; /* Divide-take value range is [0,59] * *
int tm_hour; /* Time-value range is [0,23] * *
int tm_mday; /* One months date-value range for [1,31] * *
int Tm_mon; /* Month (starting from January, 0 represents January)-the value range is [0,11] * *
int tm_year; /* Year with the value equal to the actual year minus 1900 * *
int tm_wday; /* Week – The value range is [0,6], of which 0 represents Sunday, 1 for Monday, and so on * *
int tm_yday; /* The number of days from January 1 of each year – the value interval is [0,365], of which 0 represents January 1, 1 is January 2, and so on.
int tm_isdst; /* Daylight Saving time identifier, TM_ISDST is positive when implementing daylight savings. When daylight saving time is not implemented, the TM_ISDST is 0 and TM_ISDST () is negative when the situation is not known. */
};
#define _tm_defined
#endif

The ANSI C standard says this time for using the TM structure is expressed as the decomposition time (broken-down times).

The calendar time is represented by the time_t data type, and the time (calendar time) represented by time_t is the number of seconds from one point of time (for example: January 1, 1970 0:0 0 seconds) to this point. In Time.h, we can also see that time_t is a long integer:

#ifndef _time_t_defined
typedef long time_t; /* Time Value * *
#define _TIME_T_DEFINED/* Avoid duplicate definitions time_t * *
#endif

You may be wondering: Since time_t is actually a long integer, one day in the future, from one point of time (typically January 1, 1970 0:0 0 seconds) to the number of seconds (that is, the calendar time) exceeds the range of numbers that can be represented by long shaping. For a value of the time_t data type, the time it represents cannot be as late as January 18, 2038 19:14 07 seconds. To be able to represent a longer time, some compiler vendors have introduced 64-bit or even longer cosmetic numbers to hold the calendar time. For example, Microsoft uses the __time64_t data type in Visual C + + to hold the calendar time, and through the _time64 () function to obtain the calendar time (instead of using the 32-bit word () function), This allows you to save the time before January 1, 3001 0:0 0 seconds (excluding that point) from the data type.

In the time.h header file, we can also see functions that are either time_t as parameter types or return value types:

Double Difftime (time_t time1, time_t TIME0);
time_t mktime (struct TM * timeptr);
time_t Time (time_t * timer);
char * asctime (const struct TM * timeptr);
char * CTime (const time_t *timer);

In addition, Time.h also provides two different functions to convert the calendar time (an integer represented by time_t) to the time format TM:

struct TM * gmtime (const time_t *timer);
struct TM * localtime (const time_t * timer);

By looking at MSDN, we can see that the value of the time point (the value of the Time_t object) in Microsoft C/s + + 7.0 is the number of seconds elapsed from December 31, 1899 0:0 0 seconds to that point, while the other various versions of Microsoft C + + And all different versions of Visual C + + are calculated by the number of seconds from January 1, 1970 0:0 0 seconds to that point in time.

3. Get Calendar Time

We can use the time () function to get the calendar times, which is the prototype:
time_t Time (time_t * timer);

If you have already declared the parameter timer, you can return the current calendar time from the parameter timer, and return the current calendar time by returning the value, that is, the number of seconds from one point of time (for example: January 1, 1970 0:0 0 seconds) to the present time. If the parameter is null (NUL), the function returns the current calendar time only through the return value, for example, the following example is used to display the present calendar time:

#include "time.h"
#include "stdio.h"
int main (void)
{
struct TM *ptr;
time_t lt;
Lt =time (NUL);
printf ("The Calendar time today is%d/n", lt);
return 0;
}

The results of the operation were related to the time, and the result I was running was:

The Calendar is 1122707619

1122707619 of these are the calendar times when I run the program. That is, the number of seconds from January 1, 1970 0:0 0 seconds to this time.

Get Date and time:

Here the date and time is what we usually say the year, month, day, time, minutes, seconds and other information. As we know from the 2nd section that this information is stored in a struct called TM, how do you save a calendar time as an object of a TM structure?

The functions that can be used are gmtime () and localtime (), which are the prototypes of the two functions:

struct TM * gmtime (const time_t *timer);
struct TM * localtime (const time_t * timer);

where the Gmtime () function converts the calendar time to world standard Time (that is, GMT) and returns a TM structure to hold the time, while the localtime () function converts the calendar time to local time. For example, the world standard Time obtained with the gmtime () function is July 30, 2005 7:18 20 seconds, then I use the localtime () function in China to get the local time is 8 hours later than the world standard Time, that is July 30, 2005 15:18 20 seconds. Here's an example:

#include "time.h"
#include "stdio.h"
int main (void)
{
struct TM *local;
time_t T;
T=time (NUL);
Local=localtime (&t);
printf ("Local hour are:%d/n", local->tm_hour);
Local=gmtime (&t);
printf ("UTC Hour is:%d/n", local->tm_hour);
return 0;
}

The results of the operation are:

Local hour is:15
UTC Hour Is:7

We can place the time information stored in the timeptr in the strdest-pointing string according to the format pointing to the Formatting command in the string, and maxsize characters to the strdest. The function returns the number of characters placed in the string pointed to strdest.

The operation of the function strftime () is somewhat similar to the sprintf (): The collection of format commands that begin with the percent sign (%) is recognized, and the formatted output is placed in a string. The format command describes the exact representation of the various date and time information in the string strdest. Other characters in the format string are placed in the string as is. The formatting commands are listed below, and they are case-sensitive.

%a Day of the week
%A the full name of the week
%b of the Month
Full name of%B month
Time series for the%c standard date
Rear two digits of%c year
The first day of the month in%d decimal notation
%d Month/day/year
%e in a two-character field, the first day of the month in decimal notation
%F year-month-day
Two digits of%g year, using week-based years
%G years, using weeks based
%h Abbreviated month name
%H 24-hour system hours
%I 12-hour hours
%j decimal representation of the first day of the year
Month in%m decimal notation
Number of minutes represented by%m 10 o'clock
%n New Line character
%p the equivalent display of a local AM or PM
%r 12 hours.
%R display hours and minutes: hh:mm
%s decimal number of seconds
%t Horizontal Tab
%T when displayed: Hh:mm:ss
%u days of the week, Monday is the first day (values from 0 to 6, Monday to 0)
%u week of the year, make Sunday the first day (values from 0 to 53)
%V the first weeks of the year, using a week based year
%w decimal representation of the week (values from 0 to 6, Sunday to 0)
%w Week of the year, Monday is the first day (value from 0 to 53)
The date string of the%x standard
%x Standard Time series
%y decimal Year without century (values from 0 to 99)
%Y decimal year with the century part
%z,%z the time zone name and returns a null character if the time zone name cannot be obtained.
Percent percent%

If you want to show what time it is, show it as a 12-hour system, as in the following procedure:

#include "time.h"
#include "stdio.h"
int main (void)
{
struct TM *ptr;
time_t lt;
Char str[80];
Lt=time (NUL);
Ptr=localtime (<);
Strftime (str,100, "It is now%I%p", PTR);
printf (str);
return 0;
}

The results of the operation are:
It is now 4PM

The following program displays the current full date:

#include
#include

void Main (void)
{
struct TM *newtime;
Char tmpbuf[128];
time_t LT1;
Time (<1);
Newtime=localtime (<1);
Strftime (TMPBUF, 128, "This is%A, day%d '%B in the year%y./n", NewTime);
printf (TMPBUF);
}

Run Result:

This is Saturday, and the July in the year 2005.

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.