加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C语言时间处理实例分享

发布时间:2020-12-16 03:30:29 所属栏目:百科 来源:网络整理
导读:一、简介 时间处理在编程中经常遇到,包括程序的运行时间和显示时间等。在标准C中, 日期和时间的处理包含在 time.h 的头文件中,需要使用日期和时间相关的类型的函数的话, 需要导入time.h. 二、实例 1、计算时差 #include stdio.h #include sys/time.h #in

一、简介

时间处理在编程中经常遇到,包括程序的运行时间和显示时间等。在标准C中, 日期和时间的处理包含在 time.h 的头文件中,需要使用日期和时间相关的类型的函数的话, 需要导入time.h.

二、实例

1、计算时差

#include <stdio.h>                                         
#include <sys/time.h>   
#include <unistd.h>    

int main()
{
  struct timeval start,end;
  unsigned long spend_time;

  gettimeofday( &start,NULL );
  printf("start : %d.%dn",start.tv_sec,start.tv_usec);
  sleep(1);
  gettimeofday( &end,NULL );
  printf("end  : %d.%dn",end.tv_sec,end.tv_usec);

  //微秒时差
  spend_time=1000000*(end.tv_sec-start.tv_sec)+(end.tv_usec-start.tv_usec);
  printf("%ldn",spend_time);

  return 0;
}

编译

gcc  -g -o time_diff time_diff.c
运行

以上所述就是本文的全部内容了,希望大家能够喜欢。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读