Some time functions in C language (time/sleep/clock)
[Copy link]
1.time function
Header file: #include <time.h> (I can still run it without adding this header file)
Define function: time_t time(time_t *t);
Function description: This function returns the number of seconds from 0 hours, 0 minutes, 0 seconds UTC time on January 1, 1970 to the present. If t is not a null pointer, this function will also store the return value in the memory pointed to by t pointer.
Return value: Returns the number of seconds if successful, and ((time_t)-1) value if failed, and the error cause is stored in erron.
example:
-
#include<stdio.h>
-
main()
-
{
-
int i;
-
i=time((time_t*)NULL);
-
printf("%d",i);
-
}
blog:http://blog.csdn.net/wangluojisuan/article/details/7045592
2. Sleep function (function names and function parameter units may be different on different platforms and compilers)
Header file: #include <windows.h>
Define function: unsigned sleep(unsigned seconds);
Function Description: This function execution is suspended for a period of time.
Example: (For Windows+CodeBlocks, Sleep(), the unit is ms)
-
#include<stdio.h>
-
#include<windows.h>
-
main()
-
{
-
int i,j;
-
i=time((time_t*)NULL);
-
Sleep(2000); //延迟2s
-
j=time((time_t*)NULL);
-
printf("延时了%d秒",j-i);
-
}
blog: http://blog.csdn.net/jiangxinyu/article/details/7754664
3.clock function
Function definition: clock_t clock(void);
Function Description: The time the program occupies the CPU from startup to function call.
example:
-
#include<stdio.h>
-
#include<windows.h>
-
main()
-
{
-
int i,j;
-
Sleep(2000);
-
i=clock();
-
Sleep(2000);
-
j=clock();
-
printf("开始%d\n结束%d\n经过%d\n",i,j,j-i);
-
}
|