LINUX C language programming - Time concept

xiaoxiao2021-03-05  21

This chapter goes to learn Linux time representation and calculation function 1. Time representation 2. Time measurement 3. Use of the timer ---------------------------------------- -------------------------------------------------- -------- 1. Time is indicated in the program, we often output the current time of the system, such as the output of the Date command. At this time we can use the following two functions #include time_t time (time_t * tloc); char * ctime (const time_t * Clock); Time function returns the number of seconds since January 1, 1970. Stored in the Time_T structure. However, the return value of this function has no practical meaning for us. This time we use the second function. Convert the number of seconds into a string. The return type of this function is fixed: a possible value is. THU DEC 7 14:58:59 2000 This string is fixed to 26 2. Time measurement sometimes we want to calculate the time execution. For example, we have to analyze the algorithm. This time you can use the following function. #Include int gettimeofDay (Struct Timeval * TV, Struct TimeZone * Tz); strut timeval { TV_sec; / * seconds * / long TV_usec; ​​/ * microseconds * /}; getTimeOfDay saves time in structure TV .TZ usually use null instead. #include #include #include void function () {unsigned INT I, J; Double Y; for (i = 0; i <1000; i ) for (j = 0; j <1000; j ) y = sin ((double) i);} main () {struct TimeVal TPStart , tpend; float timeuse; gettimeofday (& tpstart, NULL); function (); gettimeofday (& tpend, NULL); timeuse = 1000000 * (tpend.tv_sec-tpstart.tv_sec) tpend.tv_usec-tpstart.tv_usec; ​​timeuse / = 1000000 PRINTF ("Used Time:% F / N", TIMEUSE); EXIT (0);} This program output function execution time, we can use this to perform system performance test, or the efficiency analysis of the function algorithm. One output result on my machine is: used time: 0.556070 3.

转载请注明原文地址:https://www.9cbs.com/read-38309.html

New Post(0)