Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of the time functions clock () and time () in C language

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you about the C language time functions clock () and time () what is useful, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The time function is often used when writing code. Let's summarize the usage of the clock and time functions. The function prototype is as follows:

Clock_t _ _ cdecl clock (void); _ _ CRT_INLINE time_t _ _ cdecl time (time_t * _ Time); clock function

The clock function returns the time it takes from the start of the program to the CPU at the current calling function location, which allows you to calculate the time it takes for a piece of code to run.

Let's test it with a simple piece of code:

# include # include # include int main (int argc, char** argv) {clock_t start_t,end_t; start_t = clock (); sleep (1); end_t = clock (); printf ("d% d", start_t,end_t); return 0;}

When you first enter the program, read the time used by the current CPU through the clock function, then delay it by 1 second using the sleep function, and then read the time used by the current CPU by using the clock function, so the time difference between the two should be 1 second. The running result of the program is as follows:

As can be seen from the printed results, the CPU time of the first read is 0, the CPU time of the second read is 1000, and the delay in the program is 1 second, indicating that the time unit returned by the clock function is milliseconds (ms).

If you want to convert the time it takes to run the program in seconds, you need to calculate the time difference between the two runs, and then divide by the number of system counts per second. The number of counts per second in the system is represented by a value defined by a macro.

# define CLOCKS_PER_SEC 1000

The value of this CLOCKS_PER_SEC is 1000, which means that the CPU1 counts 1000 times per second, that is, the 1ms count once, which is the same as the result of the above analysis.

Modify the program, convert the time to seconds, and continue the test.

From the results, we can see that this cycle takes time 32ms, which is 0.032s.

Time function

The time function returns the number of seconds that have elapsed since 00:00:00 on 1970-01-01.

Let's demonstrate the use of the time function with a simple code.

# include # include # include int main (int argc, char** argv) {time_t seconds; seconds = time (NULL); printf ("seconds since 1970-01-01:% ds", seconds); return 0;}

The return value of the time function is a type of time_t, which is essentially an integer data.

The function returns the number of seconds of an integer and prints the return value as follows:

Convert this number of seconds to year, month and day by timestamp online conversion website, and the conversion URL is as follows: https://tool.lu/timestamp/:

The time after conversion is the same as the time when the program is running, indicating that the time returned by the time function is accurate, so that when you want to get the accurate time in the process of running the program, you can use the time function to obtain the exact time of the system.

The above is all the content of the article "what is the use of time functions clock () and time () in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report