原创 LINUX下获得系统时间--精确到ms

2011-6-14 21:50 8831 13 13 分类: MCU/ 嵌入式
#include <sys/time.h>
#include <stdio.h>
#include <time.h>
int main(int argc,char *argv[])
{
   struct timeval tms;
   char tstr[100];
   timerclear(&tms);
   gettimeofday(&tms,NULL);
   strftime(tstr,100,"%X",localtime(&tms.tv_sec));
   printf("%s.%d\n",tstr,tms.tv_usec/1000);/*tv_usec是微秒,除以1000转换为毫秒*/
 
  return 0;
}
=========================================================================
Linux下C语言编程--时间概念
http://linuxc.51.net 作者:hoyt (2001-05-08 11:34:12)
    这一章我们学习Linux的时间表示和计算函数 
1.时间的表示 
2.时间的测量 
3.计时器的使用 
--------------------------------------------------------------------------------


1。时间表示     在程序当中,我们经常要输出系统当前的时间,比如我们使用date命令的输出结果.这个时候我们可以使用下面两个函数 

#include 

time_t time(time_t *tloc);
char *ctime(const time_t *clock);

time函数返回从1970年1月1日0点以来的秒数.存储在time_t结构之中.不过这个函数的返回值对于我们来说没有什么实际意义.这个时候我们使用第二个函数将秒数转化为字符串. 这个函数的返回类型是固定的:一个可能值为. Thu Dec 7 14:58:59 2000 这个字符串的长度是固定的为26 
2。时间的测量     有时候我们要计算程序执行的时间.比如我们要对算法进行时间分析.这个时候可以使用下面这个函数. 

#include 

int gettimeofday(struct timeval *tv,struct timezone *tz);

strut timeval {
long tv_sec; /* 秒数 */
long tv_usec; /* 微秒数 */
};

gettimeofday将时间保存在结构tv之中.tz一般我们使用NULL来代替. 

#include #include #include 
void function()
{
 unsigned int i,j;
 double y;
 for(i=0;i<1000;i++)
 for(j=0;j<1000;j++)
   y=sin((double)i);
}

main()
{
 struct timeval tpstart,tpend;
 float  timeuse;

 gettimeofday(&tpstart,NULL);
 function();
 gettimeofday(&tpend,NULL);
 timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+
tpend.tv_usec-tpstart.tv_usec;
 timeuse/=1000000;
 printf("Used Time:%f\n",timeuse);
 exit(0);


这个程序输出函数的执行时间,我们可以使用这个来进行系统性能的测试,或者是函数算法的效率分析.在我机器上的一个输出结果是: Used Time:0.556070 
3。计时器的使用     Linux操作系统为每一个进程提供了3个内部间隔计时器. 
ITIMER_REAL:减少实际时间.到时的时候发出SIGALRM信号. 
ITIMER_VIRTUAL:减少有效时间(进程执行的时间).产生SIGVTALRM信号. 
ITIMER_PROF:减少进程的有效时间和系统时间(为进程调度用的时间).这个经常和上面一个使用用来计算系统内核时间和用户时间.产生SIGPROF信号. 
具体的操作函数是: 

#include 
int getitimer(int which,struct itimerval *value);
int setitimer(int which,struct itimerval *newval,
struct itimerval *oldval);

struct itimerval {
struct timeval it_interval;
struct timeval it_value;
}

getitimer函数得到间隔计时器的时间值.保存在value中 setitimer函数设置间隔计时器的时间值为newval.并将旧值保存在oldval中. which表示使用三个计时器中的哪一个. itimerval结构中的it_value是减少的时间,当这个值为0的时候就发出相应的信号了. 然后设置为it_interval值. 

#include 
#include 
#include 
#include 
#include 

#define  PROMPT "时间已经过去了两秒钟\n\a"

char *prompt=PROMPT;
unsigned int len;

void prompt_info(int signo)
{
  write(STDERR_FILENO,prompt,len);
}

void init_sigaction(void)
{
  struct sigaction act;
  act.sa_handler=prompt_info;
  act.sa_flags=0;
  sigemptyset(&act.sa_mask);
  sigaction(SIGPROF,&act,NULL);
}

void init_time()
{
  struct itimerval value;
  value.it_value.tv_sec=2;
  value.it_value.tv_usec=0;
  value.it_interval=value.it_value;
  setitimer(ITIMER_PROF,&value,NULL);
}

int main()
{
 len=strlen(prompt);
 init_sigaction();
 init_time();
 while(1);
 exit(0);
}

这个程序每执行两秒中之后会输出一个提示. 

=============================================================================
gettimeofday
表头文件:#include <sys/time.h>
函数原型:int gettimeofday(struct timeval *tv,struct timezone *tz)
函数说明:把目前的时间按tv所指的结构返回,当时地区的信息则放到tz所指的结构中
struct timeval
{
   long tv_sec; //秒
   long tv_usec;//微妙
}
struct timezone
{
   int tz_minuteswest;//和格林威治时间差了多少分钟
   int tz_dsttime;    //日光节约时间的状态
}
见程序:
#include <sys/time.h>
#include <stdio.h>

int main()
{
     struct timeval tv;
     struct timezone tz;
     gettimeofday(&tv,&tz);
     printf("tv.tv_sec:%d\n",tv.tv_sec);
     printf("tv.tv_usec:%d\n",tv.tv_usec);
     printf("tz.tz_minuteswest:%d\n",tz.tz_minuteswest);
     printf("tv.tz_dsttime:%d\n",tz.tz_dsttime);
     return 0;
}
程序结果:
tv.tv_sec:1252081843
tv.tv_usec:119409
tz.tz_minuteswest:-480
tv.tz_dsttime:0
==============================================================================
获取linux时间
获得时间有以下两种方法
1、使用 time_t   *time(time_t   *)函数,它返回一个time_t指针,保存着从1970110点以来的秒数  
然后使用 char   *ctime(   const   time_t *)就可以得到类似date命令显示的日期格式字符串了。例子:  
//------------------------用time()的例子-----------------------
#include <time.h>  
int main()  
{  
    time_t tmNow = 0;  
  
    time(&tmNow);//
    printf("%ld,%s", tmNow, ctime(&tmNow));  //打印如“Mon Sep  7 13:20:04 2009”信息
 
    exit(0);  
}  
//------------------------------------------------------------
2、调用gettimeofday将时间写入timeval结构中,这种方法精确到微妙
#include <sys/time.h>
int gettimeofday (struct timeval *__restrict __tv, __timezone_ptr_t __tz)
struct timeval
{
    __time_t tv_sec;            /* Seconds.  */
    long int tv_usec;           /* useconds.  */
};
其中  
tv_sec   保存着从1970110点以来的秒数  
tv_usec  保存微秒数  
这时也可以调用ctime函数将tv_sec的内容传入,同样可得到类dete命令显示的字符串  
一般timeval结构常用于计算进程运行的时间,主要是因为它的精度高。
还有一个更高精度的结构,这是linux精确时间结构
#include <time.h>
struct timespec
{   time_t tv_sec;
    long int tv_nsec;  /*Nanoseconds*/
}
  
补充一点: time_t 在大多数系统中等于 long
//---------------------用timeval计算运行时间的例子-----------------------
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
int isprime_num(int i);
int main()
{
        struct timeval t1;
        struct timeval t2;
        float elapse;
        int i;
        int count=0;
        //get the count prime number in 0..10000
        //start timing
        gettimeofday(&t1,NULL);

        for(i=0;i<=10000000;++i)
        {       if(isprime_num(i)==1)
                        count++;
        }
        //stop timing
        gettimeofday(&t2,NULL);
        elapse=1000000*(t2.tv_sec-t1.tv_sec)+t2.tv_usec-t1.tv_usec;
        elapse/=1000000;
        printf("the count of prime number is %d\n",count);
        printf("elapse time %f\n",(elapse));

        return 0;
}
//i是否素数
//出口:返回1表示是素数,0不是
int isprime_num(int i)
{
        int j;
        int top=(int)sqrt((float)i);
        for(j=2;j<=top;++j)
        {       if(i%j==0)
                        return 0;
        }
        return 1;
}
//-----------------------------------------------------------------
http://blog.chinaunix.net/u3/107023/showart_2107163.html
PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
13
关闭 站长推荐上一条 /3 下一条