Linux precise timing, and commonly used C-time functions _ Android

Source: Internet
Author: User
Tags local time string format time and date

precise timing in microseconds

Long Long l1,l2,l3;

Timeval TV1;

Take a Time

Gettimeofday (&tv,null);

L1 = tv.tv_sec*1000*1000 + tv.tv_usec;

Execute some code, such as algorithm 1 ...

Second time taken

Gettimeofday (&tv,null);

L2 = tv.tv_sec*1000*1000+tv.tv_usec;

Execute some code, such as algorithm 2 ...

Gettimeofday (&tv,null);

L3 = tv.tv_sec*1000*1000+tv.tv_usec;

printf ("%lld%lld/n", L2-L1, L3-L2);

You can see the execution time of two algorithms in microseconds (one out of 10,000 seconds).

Timings in seconds

Asctime (representing time and date in string format)

Related functions

Time,ctime,gmtime,localtime

Table header File

#include <time.h>

Defining functions

char * asctime (const struct TM * timeptr);

Function description

Asctime () Converts the information in the TM structure referred to in the parameter timeptr to the time date representation method used in the real world, and then returns the result in string form. This function has been converted to local time by the time zone, and the string is in the format: "Wed June 21:49:08 1993/n"

return value

This string may be corrupted if the relevant time-date function is called again. This function differs from CTime in that the parameters passed in are different structures.

Additional Instructions

Returns a string representing the current local time date.

Example

#include <time.h>
Main ()
{
time_t TIMEP;
Time (&TIMEP);
printf ("%s", Asctime (Gmtime (&TIMEP));
}

Perform

Sat Oct 28 02:10:06 2000

CTime (representing time and date in string format)

Related functions

Time,asctime,gmtime,localtime

Table header File

#include <time.h>

Defining functions

Char *ctime (const time_t *TIMEP);

Function description

CTime () Converts the information in the time_t structure of the argument TIMEP to the time-date representation used in the real world, and then returns the result as a string form. This function has been converted to local time by the time zone, and the string is in the form "Wed June 21:49:08 1993/n". This string may be corrupted if the relevant time-date function is called again.

return value

Returns a string representing the current local time date.

Example

#include <time.h>
Main ()
{
time_t TIMEP;
Time (&TIMEP);
printf ("%s", CTime (&TIMEP));
}

Perform

Sat Oct 28 10:12:05 2000

Gettimeofday (Get the current time)

Related functions

Time,ctime,ftime,settimeofday

Table header File

#include <sys/time.h>
#include <unistd.h>

Defining functions

int gettimeofday (struct timeval * TV, struct timezone * tz)

Function description

Gettimeofday () returns the current time with the structure that the TV refers to, and the local time zone information is placed in the structure referred to by TZ.
The TIMEVAL structure is defined as:
struct timeval{
Long tv_sec; /* SEC * *
Long tv_usec; /* microsecond * *
};
The timezone structure is defined as:
struct timezone{
int tz_minuteswest; /* and Greenwich TIME DIFFERENCE how many minutes * *
int tz_dsttime; * The state of Daylight saving time * *
};
Both of these structures are defined in/usr/include/sys/time.h. The state represented by Tz_dsttime is as follows
Dst_none/* Do not use * *
DST_USA/* USA * *
Dst_aust/* Australia * *
Dst_wet/* Western Europe * *
Dst_met/* Central Europe *
Dst_eet/* Eastern Europe *
Dst_can/* Canada * *
DST_GB/* Britannia *
Dst_rum/* Romania * *
Dst_tur/* Turkey * *
Dst_austalt/* Australia (after 1986) * *

return value

Success returns 0, the failure returns 1, and the error code is stored in errno. Additional instructions The memory space referred to by efault Pointer TV and TZ exceeds access permissions.

Example

#include <sys/time.h>
#include <unistd.h>
Main () {
struct Timeval TV;
struct timezone tz;
Gettimeofday (&TV, &tz);
printf ("TV_SEC; %d/n ", tv,.tv_sec);
printf ("TV_USEC; %d/n ", tv.tv_usec);
printf ("Tz_minuteswest; %d/n ", tz.tz_minuteswest);
printf ("Tz_dsttime,%d/n", tz.tz_dsttime);
}

Perform

tv_sec:974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0

gmtime (get current time and date)

Related functions

Time,asctime,ctime,localtime

Table header File

#include <time.h>

Defining functions

struct Tm*gmtime (const TIME_T*TIMEP);

Function description

Gmtime () Converts the information in the time_t structure of the parameter TIMEP to the time-date representation used in the real world, and then returns the result from the structure TM.
The structure TM is defined as
struct TM
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int Tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
int TM_SEC represents the current number of seconds, with a normal range of 0-59, but allowed to 61 seconds
int Tm_min represents current score, range 0-59
int Tm_hour The number of hours from midnight, with a range of 0-23
int Tm_mday The number of days of the current month, range 01-31
int Tm_mon represents the current month, starting from January, ranging from 0-11
int tm_year number of years since 1900
int Tm_wday The number of days in a week, starting from Monday, the range is 0-6
int Tm_yday The number of days since January 1 this year, with a range of 0-365
int TM_ISDST flag for daylight saving time
The date returned by this function is not time zone conversion, but UTC time.

return value

Return Structure TM represents current UTC time

Example

#include <time.h>
Main () {
Char *wday[]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_t TIMEP;
struct TM *p;
Time (&TIMEP);
P=gmtime (&TIMEP);
printf ("%d%d%d", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday);
printf ("%s%d;%d;%d/n", Wday[p->tm_wday], P->tm_hour, P->tm_min, p->tm_sec);
}

Perform

2000/10/28 Sat 8:15:38

localtime (get local current time and date)

Related functions

Time, Asctime, CTime, gmtime

Table header File

#include <time.h>

Defining functions

struct TM *localtime (const time_t * TIMEP);

Function description

LocalTime () Converts the information in the time_t structure of the parameter TIMEP to the time-date representation used in the real world, and then returns the result from the structure TM. Please refer to Gmtime () for the definition of structure TM. The time date returned by this function has been converted to a local time zone.

return value

Returns the structure TM representing the current local time.

Example

#include <time.h>
Main () {
Char *wday[]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_t TIMEP;
struct TM *p;
Time (&TIMEP);
P=localtime (&TIMEP); * * Get local time/
printf ("%d%d%d", (1900+p->tm_year), (L+p->tm_mon), p->tm_mday);
printf ("%s%d:%d:%d/n", Wday[p->tm_wday],p->tm_hour, P->tm_min, p->tm_sec);
}

Perform

2000/10/28 Sat 11:12:22

Mktime (converts time structure data to elapsed seconds)

Related functions

Time,asctime,gmtime,localtime

Table header File

#include <time.h>

Defining functions

time_t mktime (Strcut TM * timeptr);

Function description

Mktime () is used to convert the TM structure data referred to by the parameter timeptr to the number of seconds elapsed since the UTC time of 0 seconds from January 1, 1970 A.D.

return value

Returns the number of seconds elapsed.

Example

/* Use time () to obtain the Times (seconds), using localtime ()
Convert to struct TM reuse Mktine () convert struct TM to original number of seconds * *
#include <time.h>
Main ()
{
time_t TIMEP;
Strcut TM *p;
Time (&TIMEP);
printf ("Time ():%d/n", TIMEP);
P=localtime (&TIMEP);
TIMEP = Mktime (p);
printf ("Time ()->localtime ()->mktime ():%d/n", TIMEP);
}

Perform

Time (): 974943297
Time ()->localtime ()->mktime (): 974943297

Settimeofday (set current time)

Related functions

Time,ctime,ftime,gettimeofday

Table header File

#include <sys/time.h>
#include <unistd.h>

Defining functions

int settimeofday (const struct timeval *tv,const struct timezone);

Function description

Settimeofday () Sets the current time to the structure information that the TV refers to, and the local time zone information is set to the structure referred to by the TZ. For detailed instructions please refer to gettimeofday (). Note that only the root permission can be used to modify the time.

return value

Success returns 0, the failure returns 1, and the error code is stored in errno.

Error code

Eperm is not called Settimeofday () by root permission, and permissions are not sufficient.
Einval time zone or some data is incorrect and cannot be set correctly.

time (to get current)

Related functions

Ctime,ftime,gettimeofday

Table header File

#include <time.h>

Defining functions

time_t time (time_t *t);

Function description

This function returns the number of seconds elapsed from 0:0 UTC, January 1, 1970, from 0 seconds to the current time. If T is not a null pointer, this function also saves the return value to the memory referred to by the T pointer.

return value

Success returns the number of seconds, Failure returns ((time_t)-1) value, and the reason for the error is in errno.

Example

#include <time.h>
Mian ()
{
int seconds= time ((time_t*) NULL);
printf ("%d/n", seconds);
}

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.