Time Difference obtained in Linux, accurate to microseconds

Source: Internet
Author: User

 

The time required for calculating the FFT in software recently. The collected references are as follows:

# Include <stdio. h>
# Include <stdlib. h>/* contains the standard library header file */

# Include <sys/time. h>

Int main (INT argc, char ** argv)
{
Struct timeval start, stop, diff;
Gettimeofday (& START, 0 );
// Do what you want to do...
Gettimeofday (& stop, 0 );
Timeval_subtract (& diff, & START, & stop );
Printf ("Total time: % d microsecond \ n", diff. TV _usec );
}

/**
* Calculate the two time intervals to obtain the time difference.
* @ Param struct timeval * resule returns the calculated time.
* @ Param struct timeval * x the previous time to be calculated
* @ Param struct timeval * y the last time to be calculated
* Return-1 failure, 0 success
**/
Int timeval_subtract (struct timeval * result, struct timeval * X, struct timeval * Y)

{
Int nsec;

If (X-> TV _sec> Y-> TV _sec)
Return-1;

If (X-> TV _sec = Y-> TV _sec) & (X-> TV _usec> Y-> TV _usec ))
Return-1;

Result-> TV _sec = (Y-> TV _sec-x-> TV _sec );
Result-> TV _usec = (Y-> TV _usec-x-> TV _usec );

If (result-> TV _usec <0)
{
Result-> TV _sec --;
Result-> maid + = 1000000;
}

Return 0;
}

--------------------------------------------------
# Include <sys/time. h>
# Include <unistd. h>
Int gettimeofday (struct timeval * TV, struct timezone * tz );
Function: returns the current time in the structure specified by TV.
Struct timeval {
Long TV _sec; // second
Long TV _usec; // microseconds
};
Convert TV _usec to milliseconds.
Check the timezone structure.

Appendix:
-------------------------------------------------
How to Implement C language for obtaining system time in Linux
# Include <time. h> // header file of C Language
# Include <stdio. h> // I/O of C Language

Void main ()
{
Time_t now; // instantiate the time_t Structure
Struct TM * timenow; // instantiate the TM structure pointer
Time (& now );
// The time function reads the current time (International Standard Time is not Beijing time), and then transmits the value to now

Timenow = localtime (& now );
// The localtime function converts the time now obtained from time to the time in your computer (that is, the region you set)

Printf ("local time is % s \ n", asctime (timenow ));
// In the previous sentence, the asctime function converts the time into characters and outputs the time using the printf () function.
}

Note: time_t is a struct defined in time. h. The prototype of the TM struct is as follows:

Struct TM
{
Int tm_sec; // seconds 0-61
Int tm_min; // minutes 1-59
Int tm_hour; // hours 0-23
Int tm_mday; // day of the month 1-31
Int tm_mon; // months since Jan 0-11
Int tm_year; // years from 1900
Int tm_wday; // days since Sunday, 0-6
Int tm_yday; // days since Jan 1, 0-365
Int tm_isdst; // Daylight Saving Time indicator
};

 

 

Http://szfzafa.blog.163.com/blog/static/11895416720125264621443/

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.