获取系统时间,精确到微秒

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

int main() {
    struct timeval start, end;
    gettimeofday( &start, NULL );
    sleep(3);
    gettimeofday( &end, NULL );
    int timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
    printf("time: %d us\n", timeuse);
    return 0;
}java


实验结果:windows

在作测试或性能优化时,常常要知道程序运行的时间,在Linux系统能够使用time命令来计算程序运行运行所消耗的时间,能精确到毫秒,若是要精 确到代码块或某个操做运行时所消耗的时间,time命令就不给力了。若是对时间的精度要求不高的话,能够调用标准C的接口time来获得开始和结束的时 间,再调用difftime接口来计算时间差,精度是秒,代码以下所示:性能优化

下载: time.c性能

  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. int main(){
  5.     time_t t_start, t_end;
  6.     t_start = time(NULL) ;
  7.     sleep(3000);
  8.     t_end = time(NULL) ;
  9.     printf("time: %.0f s\n", difftime(t_end,t_start)) ;
  10.     return 0;
  11. }

若是要让程序休眠3秒,Windows使用Sleep(3000),Linux使用sleep(3),即Windows的Sleep接口的参数的单位是毫秒,Linux的sleep接口的参数的单位是秒。

若是须要精确到毫秒,以上程序就发挥不了做用,若是在Java要达到这要求就很简单了,代码以下所示:测试

下载: Time.java优化

  1. public class Time {
  2.     public static void main(String[] args) {
  3.         try {
  4.             long startTime = System.currentTimeMillis();
  5.             Thread.sleep(3000);
  6.             long endTime = System.currentTimeMillis();
  7.             System.out.println("time: " + (endTime - startTime) + " ms");
  8.         } catch (InterruptedException e) {
  9.             e.printStackTrace();
  10.         }
  11.     }
  12. }

经过Google找了一些资料后,发现C语言里没有标准的接口能够得到精确到毫秒的时间,都会调用到与操做系统相关的API,下面会分别介绍在Linux和Windows系统下的多种实现方法,但愿对你们有帮助。spa

使用gettimeofday接口:操作系统

下载: gettimeofday.ccode

  1. #include <stdio.h>
  2. #include <sys/time.h>
  3.  
  4. int main() {
  5.     struct timeval start, end;
  6.     gettimeofday( &start, NULL );
  7.     sleep(3);
  8.     gettimeofday( &end, NULL );
  9.     int timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
  10.     printf("time: %d us\n", timeuse);
  11.     return 0;
  12. }

gettimeofday能获得微秒数,比毫秒还要更精确。

使用ftime接口:

下载: ftime.c

  1. #include <stdio.h>
  2. #include <sys/timeb.h>
  3.  
  4. long long getSystemTime() {
  5.     struct timeb t;
  6.     ftime(&t);
  7.     return 1000 * t.time + t.millitm;
  8. }
  9.  
  10. int main() {
  11.     long long start=getSystemTime();
  12.     sleep(3);
  13.     long long end=getSystemTime();
  14.  
  15.     printf("time: %lld ms\n", end-start);
  16.     return 0;
  17. }

Windows系统

使用GetTickCount接口:

下载: GetTickCount.c

  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. int main() {
  5.     DWORD start, stop;
  6.     start = GetTickCount();
  7.     Sleep(3000);
  8.     stop = GetTickCount();
  9.     printf("time: %lld ms\n", stop - start);
  10.     return 0;
  11. }

Windows系统下有些编译器使用printf输出64位整数参数要使用%I64d,好比VC。

使用QueryPerformanceX接口:

下载: QueryPerformance.c

  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. int main(){
  5.     LARGE_INTEGER li;
  6.     LONGLONG start, end, freq;
  7.     QueryPerformanceFrequency(&li);
  8.     freq = li.QuadPart;
  9.     QueryPerformanceCounter(&li);
  10.     start = li.QuadPart;
  11.     Sleep(3000);
  12.     QueryPerformanceCounter(&li);
  13.     end = li.QuadPart;
  14.     int useTime =(int)((end - start) * 1000 / freq);
  15.     printf("time: %d ms\n", useTime);
  16.     return 0;
  17. }

使用GetSystemTime接口:

下载: GetSystemTime.c

  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. int main(){
  5.     SYSTEMTIME currentTime;
  6.     GetSystemTime(&currentTime);
  7.     printf("time: %u/%u/%u %u:%u:%u:%u %d\n",           
  8.      currentTime.wYear,currentTime.wMonth,currentTime.wDay,
  9.      currentTime.wHour,currentTime.wMinute,currentTime.wSecond,
  10.      currentTime.wMilliseconds,currentTime.wDayOfWeek);
  11.     return 0;
  12. }

这种方法没给出计算时间差的实现,只给出如何用GetSystemTime调用获得当前时间,计算时间差比较简单,根据年、月、日、时、分秒和毫秒计算出一个整数,再将两整数相减便可。

后记

以上是经过Google找到一些用C语言得到精确到毫秒的实现方法,对比Linux和Windows的方法,发现两个系统的API命名很不同,Linux接口名要么都是小写要么使用下划线(_)来分隔单词,而Windows接口名中的单词首字母大写。

相关文章
相关标签/搜索