当前位置:网站首页>Count the time-consuming duration of an operation (function)

Count the time-consuming duration of an operation (function)

2022-06-11 06:23:00 Tinghua_ M

C Medium statistics time

stay C Functions can be used in languages gettimeofday() Function to get the time . Its precision can be as subtle as . Let's look at it first man Description in the manual :

$ man gettimeofday

DESCRIPTION
       The functions gettimeofday() and settimeofday() can get and set the time as well as a timezone.
       The tv argument is a struct timeval (as specified in <sys/time.h>):

           struct timeval {
               time_t      tv_sec;     /* seconds */
               suseconds_t tv_usec;    /* microseconds */
           };

  

  This means that the function returns timeval Time value of type ,tv_sec Is the second ,tv_usec It represents microseconds .

give an example

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


int main(int argc,  char * argv[])
{
    struct timeval tpstart,tpend;

 
    gettimeofday(&tpstart,NULL);
    seek("/sdcard/Music/mm.mp3", 0, 3600);
    gettimeofday(&tpend,NULL);

    printf("Used Time:%f\n",tpend.tv_usec - tpstart.tv_usec);

    return 0;
}

This example is to debug a self defined seek function , You can also replace it with the function you want to debug , Two are defined timeval The variable of tpstart,tpend, Record the time when the function starts and ends , Subtract the two , You can calculate the time consumption of this function . What I'm printing here is tv_usec( Microsecond ), You can also print according to your own needs tv_sec.

explain :

In the use of gettimeofday() Function time , The second parameter is generally empty , Because we usually just want to get the current time , Without getting timezone The numerical

原网站

版权声明
本文为[Tinghua_ M]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110620187781.html