Chinaunix首页 | 论坛 | 博客
  • 博客访问: 565666
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: C/C++

2015-02-27 17:14:02

1. struct timeval ;

header file #include
struct timeval
{
    time_t   tv_sec ;   /* seconds time_t is typedef  of  long int */ 
    long int  tv_usec ; /*  microseconds */ 
} ;

operations :

gettimeofday 
函数原型:
int gettimeofday ( struct timeval *tv , struct timezone *tz ) ;

param1 : struct timeval * 将主调方法中创建好的 struct timeval 变量指针传入至方法中,
方法将会获取当前的时间距 1970/1/1/ 00:00 时间之间的 (秒,毫秒) 并将数值置入
传入的 struct timeval 指针所指向的空间中

param2 : struct timezone * 该参数主要用于从 gettimeofday 方法中获取当前的时区信息,
并以 值-地址 的方式 返回给主调方法中,如果将该参数中传入 NULL, 则不会进行获取
时区信息并返回的操作



代码实例:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <sys/time.h>
  3. #include <time.h>

  4. int main ( int argc , char ** argv )
  5. {
  6.         struct timeval tv ;

  7.         gettimeofday (&tv , NULL) ;
  8.         printf ("time %u : %u", tv.tv_sec , tv.tv_usec ) ;
  9.         return 0 ;
  10. }



2. struct timespec ;
header file #include
struct timespec 
{
    time_t tv_sec ;    // seconds , time_t is type of long 
    long    tv_nsec ; //  naonseconds  
} ;

operations : clock_gettime 
函数原型

int clock_gettime ( clockid_t clkid , struct timespec *tp ) ;

param1:   clockid_t 
是用来指定时钟时间类型,传入的是一系列定义好的宏变量:
{
 CLOCK_REALTIME: 它是系统实时时间,从 UTC1970/1/1/00:00:00 开始计时

CLOCK_MONOTONIC:   它代表的是将系统启动时间作为起始时间开始计时
 
CLOCK_PROCESS_CPUTIME_ID: 它代表的是正在执行当前代码的进程,执行到当前代码段CPU所花费的时间

CLOCK_THREAD_CPUTIME_ID:  它表示的是当前线程执行到当前代码段所消耗的CPU时间
}

param2: struct timespec *
该参数是用于系统根据传入的第一个参数所代表的类型来获取当前的时间,
并将当前时间换算成符合 struct timespec 变量的类型,根据传入的指向主调方法中的
struct timespec 变量的指针所指向的空间,为其赋值。
之后主调方法便可通过传入 clock_gettime 方法中的指针获得当前所需要查询的时间类型

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <time.h>

  3. int main ( int argc , char ** argv )
  4. {
  5.         struct timespec time = {0,0};

  6.         // CLOCK_REALTIME
  7.         clock_gettime ( CLOCK_REALTIME , &time ) ;
  8.         printf ("CLOCK_REALTIME %d---> %d \n", time.tv_sec, time.tv_nsec) ;

  9.         // CLOCK_MONOTONIC
  10.         clock_gettime (CLOCK_MONOTONIC , &time ) ;
  11.         printf (" CLOCK_MONOTONIC %d --- > %d \n" , time.tv_sec , time.tv_nsec) ;

  12.         // CLOCK_PROCESS_CPUTIME_ID
  13.         clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &time ) ;
  14.         printf ( "CLOCK_PROCESS_CPUTIME %d ---> %d \n", time.tv_sec , time.tv_nsec ) ;

  15.         // CLOCK THREAD CPUTIME ID
  16.         clock_gettime (CLOCK_THREAD_CPUTIME_ID , &time ) ;
  17.         printf ("CLOCK_THREAD_CPUTIME_ID %d ---> %d \n ", time.tv_sec , time.tv_nsec ) ;

  18.         sleep (5) ;

  19.         return 0 ;
  20. }
  21. ~
  22. ~
2.1
阅读(1014) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~