How to use the Gettimeofday () function of the Linux time function

Source: Internet
Author: User
Tags diff local time

1. Introduction:

You can use the function gettimeofday () function to get the time in C language. Its precision can be achieved by subtle

2. Function Prototypes:

#include <sys/time.h>

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

3. Description:

Gettimeofday () returns the current time with the TV structure, and the local time zone information is placed in the structure that TZ refers to.

4. Structure:

1>timeval

struct timeval{

Long tv_sec;/* sec */

Long tv_usec;/* Subtle */

};

The 2>timezone structure is defined as:

struct timezone{

int tz_minuteswest;/* and Greenwich TIME DIFFERENCE how many minutes */

int Tz_dsttime;/*type of DST correction*/

}

3> in the Gettimeofday () function, either TV or TZ can be empty. If it is empty, it does not return its corresponding struct.

The 4> function returns 0 after successful execution, and 1 after the failure, and the error code is stored in errno.

5. Program Examples:

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

#include <unistd.h>

int 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);

}

Note: When using the Gettimeofday () function, the second parameter is generally empty, since we are generally just trying to get the current time without having to get the timezone value

Two. A common method of the Gettimeofday () function

When testing a program, you often need to know the time it takes for the program to execute, and in Linux you can use the function gettimeofday to get the time.

1. Program Examples:

The time required to execute the test call to the Delya () function (in subtle units)

#include <stdio.h>

#include <sys/time.h>

#include <unistd.h>

int delay (int time)

{

int i,j;

for (i =0;i<time;i++)

for (j=0;j<5000;j++)

;

}

int main ()

{

struct Timeval start;

struct Timeval end;

unsigned long diff;

Gettimeofday (&start,null);

Delay (10);

Gettimeofday (&end,null);

diff = 1000000 * (end.tv_sec-start.tv_sec) + end.tv_usec-start.tv_usec;

printf ("Thedifference is%ld\n", diff);

return 0;

}

Description

Using this method, you can detect the time that is used to call the delay () function

How to use the Gettimeofday () function of the Linux time function

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.