2121 views|0 replies

3836

Posts

19

Resources
The OP
 

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:


  1. #include<stdio.h>

  2. main()

  3. {

  4. int i;

  5. i=time((time_t*)NULL);

  6. printf("%d",i);

  7. }

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)

  1. #include<stdio.h>

  2. #include<windows.h>

  3. main()

  4. {

  5. int i,j;

  6. i=time((time_t*)NULL);

  7. Sleep(2000); //延迟2s

  8. j=time((time_t*)NULL);

  9. printf("延时了%d秒",j-i);

  10. }

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:

  1. #include<stdio.h>

  2. #include<windows.h>

  3. main()

  4. {

  5. int i,j;

  6. Sleep(2000);

  7. i=clock();

  8. Sleep(2000);

  9. j=clock();

  10. printf("开始%d\n结束%d\n经过%d\n",i,j,j-i);

  11. }

This post is from DSP and ARM Processors
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list