Linux Statistics program run time

Source: Internet
Author: User

Test code run time

<sys/time.h> in Linux has a function to get the current time, accurate to microseconds----> gettimeofday ()

1#include <sys/time.h>       // int gettimeofday (struct timeval *tv, struct timezone *tz);2     /*********************************************3 * struct Timeval4      * {5 * time_t tv_sec; Seconds6 * suseconds_t tv_usec; Microseconds: microsecond 10^ ( -6) s, where the tv_sec is in microseconds7 *//Millisecond: Ms 10^ ( -3) s8      * }9      **********************************************Ten * struct TimeZone One      * { A * int tz_minuteswest; Minutes west of Greenwich - * int tz_dsttime; Type of DST correction -      * } the      **********************************************/

When used, define two struct timeval variables (usually the second parameter of the Gettimeofday () is set to NULL), save the time before and after the code test, and finally subtract, to get the code run time (can be converted to the time you need).

1#include <stdio.h>2#include <sys/time.h>//For Gettimeofday ()3#include <string.h>//For memset ()4 5 intMain ()6 {7     inti =10000000;8     struct timeval start, end;//define 2 struct Timeval variables9 Ten     //------------------------- One     gettimeofday (&start, NULL);//get the beginning time A     //------------------------- -      -     //Test Code the      while(i) -     { -i--; -     } +  -     //------------------------- +     gettimeofday (&end, NULL);//Get the end time A     //------------------------- at      -     Long LongTotal_time = (end.tv_sec-start.tv_sec) *1000000+ (END.TV_USEC-START.TV_USEC);//get the Run time by microsecond -printf"Total time is %lld us\n", total_time); -Total_time/= +;//get the run time by millisecond -printf"Total time is %lld ms\n", total_time); -}

Test Result: (CentOS 6.5, gcc 4.4.7)
Total time is 49658 us
Total time is MS
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.