Chinaunix首页 | 论坛 | 博客
  • 博客访问: 827470
  • 博文数量: 143
  • 博客积分: 455
  • 博客等级: 一等列兵
  • 技术积分: 861
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-03 00:11
文章分类

全部博文(143)

文章存档

2018年(10)

2017年(6)

2016年(28)

2015年(14)

2014年(67)

2013年(1)

2012年(17)

我的朋友

分类: LINUX

2014-02-10 18:08:56

最近需要测算软件计算FFT所需要的时间,所收集参考的资料:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2.   #include <stdlib.h> /* 包含标准库头文件 */
  3.   #include <sys/time.h>
  4.     
  5.   int main(int argc, char **argv)
  6.   {
  7.   struct timeval start,stop,diff;
  8.   gettimeofday(&start,0);
  9.                     //做你要做的事...
  10.   gettimeofday(&stop,0);
  11.   timeval_subtract(&diff,&start,&stop);
  12.                     printf("总计用时:%d 微秒\n",diff.tv_usec);
  13.   }
  14.     
  15.   /**
  16.       * 计算两个时间的间隔,得到时间差
  17.       * @param struct timeval* resule 返回计算出来的时间
  18.       * @param struct timeval* x 需要计算的前一个时间
  19.       * @param struct timeval* y 需要计算的后一个时间
  20.       * return -1 failure ,0 success
  21.   **/
  22.   int timeval_subtract(struct timeval* result, struct timeval* x, struct timeval* y)
  23.   {
  24.         int nsec;
  25.     
  26.         if ( x->tv_sec>y->tv_sec )
  27.                   return -1;
  28.     
  29.         if ( (x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec) )
  30.                   return -1;
  31.     
  32.         result->tv_sec = ( y->tv_sec-x->tv_sec );
  33.         result->tv_usec = ( y->tv_usec-x->tv_usec );
  34.     
  35.         if (result->tv_usec<0)
  36.         {
  37.                   result->tv_sec--;
  38.                   result->tv_usec+=1000000;
  39.         }
  40.     
  41.         return 0;
  42.   }

 

点击(此处)折叠或打开

  1. --------------------------------------------------
  2. #include <sys/time.h>
  3.   #include <unistd.h>
  4.   int gettimeofday(struct timeval *tv,struct timezone *tz);
  5.   功能:将目前的时间以tv所指的结构返回。
  6.   struct timeval{
  7.           long tv_sec;//
  8.           long tv_usec;//微秒
  9.   }
  10.   将其中的tv_usec转换到毫秒即可。
  11.   timezone结构自己查吧
  12.  
  13.  
  14.  
  15. 附录:
  16. -------------------------------------------------
  17. Linux下获得系统时间的C语言的实现方法
  18.   #include<time.h> //C语言的头文件
  19.   #include<stdio.h> //C语言的I/O
  20.     
  21.   void main()
  22.   {
  23.   time_t now; //实例化time_t结构
  24.   struct tm *timenow; //实例化tm结构指针
  25.   time(&now);
  26.   //time函数读取现在的时间(国际标准时间非北京时间),然后传值给now
  27.     
  28.   timenow = localtime(&now);
  29.   //localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)
  30.     
  31.   printf("Local time is %s\n",asctime(timenow));
  32.   //上句中asctime函数把时间转换成字符,通过printf()函数输出
  33.   }
  34.     
  35.   注释:time_t是一个在time.h中定义好的结构体。而tm结构体的原形如下:
  36.     
  37.   struct tm
  38.   {
  39.       int tm_sec;//seconds 0-61
  40.       int tm_min;//minutes 1-59
  41.       int tm_hour;//hours 0-23
  42.       int tm_mday;//day of the month 1-31
  43.       int tm_mon;//months since jan 0-11
  44.       int tm_year;//years from 1900
  45.       int tm_wday;//days since Sunday, 0-6
  46.       int tm_yday;//days since Jan 1, 0-365
  47.       int tm_isdst;//Daylight Saving time indicator
  48.   };

阅读(13279) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~