C++/C get system time, get program execution time

Source: Internet
Author: User
Tags local time month name

Personally think the second kind is more practical, but also the most commonly used ~

However, when the calculation algorithm time-consuming, do not forget second, not as long as the milliseconds to reduce, otherwise negative, if the algorithm time is too long to use minutes. Otherwise, hours ...

Scenario-Advantages: Use only C standard library; Disadvantage: only accurate to second level


#include <time.h>
#include <stdio.h>
int main (void)
{
time_t t = time (0);
Char tmp[64];
Strftime (TMP, sizeof (TMP), "%y/%m/%d%x%A year%j days%z", LocalTime (&t));
Puts (TMP);
return 0;
}
size_t strftime (char *strdest, size_t maxsize, const char *format, const struct TM *timeptr);
Generates a string based on a format string.
struct TM *localtime (const time_t *timer);
Get local time, LocalTime gets the results returned by the Fabric TM
The returned string can be in the following format:
%a abbreviation of the week. Eg:tue
%A the full name of the week. Eg:tuesday
The abbreviation for the%b month name.
The full name of the%B month name.
The%c local end datetime is a better representation of the string.
%d numbers indicate the day ordinal of the month (range 00 to 31). Date
The%H uses a 24-hour number to denote the number of hours (ranging from 00 to 23).
%I A 12-hour number indicates the number of hours (range 01 to 12).
%j indicates the day of the Year in Numbers (range 001 to 366).
Number of%m months (range from 1 to 12).
%M minutes.
%p indicates local time with ' AM ' or ' PM '.
%s Number of seconds.
The number of%u is expressed as the week of the current year, and the first one weeks start in Sunday.
The%W number is expressed as the week of the current year, and the first one weeks start in Monday.
%w numbers indicate the day of the week (0 is Sunday).
%x does not include date notation for time.
%x does not include a time representation of the date. Eg:15:26:30
The%y two digits represent the year (range from 00 to 99).
%Y a full year number representation, that is, four digits. eg:2008
%Z (%Z) time zone or name abbreviation. EG: China Standard Time
Percent% character.

Scenario Two advantages: can be accurate to the millisecond level; Cons: Using the Windows API
#include <windows.h>
#include <stdio.h>
int main (void)
{
SYSTEMTIME SYS;
Getlocaltime (&sys);
printf ("%4d/%02d/%02d%02d:%02d:%02d.%0 3d Week%1d\n ", Sys.wyear,sys.wmonth,sys.wday,sys.whour,sys.wminute, Sys.wsecond,sys.wmilliseconds,sys.wdayofweek);
return 0;
}
Scenario three, advantages: using System functions, can also modify the system time
This file must be a C + + file
#include <stdlib.h>
#include <iostream>
using namespace Std;
void Main ()
{
System ("Time");
}
Scenario four, convert the current time to a second level, and then the corresponding time conversion can be
This file must be a C + + file
#include <iostream>
#include <ctime>
using namespace Std;
int main ()
{
time_t Now_time;
Now_time = time (NULL);
cout<<now_time;
return 0;
}

C++/C get system time, get program execution time

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.