程序运行时间计算

以前经常听人提起如何计算程序运行时间,给出一系列函数,当时没有注意,随便选了clock()最简单的方式进行计算。等到真正需要检测程序性能提升了多少,才发现这里面有很多要注意的地方。

最常用的的方式:
#include
time_t start = clock();
time_t end = clock();
printf("the running time is : %f\n", double(end -begin)/CLOCKS_PER_SEC);
clock()计算的是CPU执行耗时,注意是CPU!如果有多个核并行,最后的结果是每个CPU上运算时间的总和!想要精确到毫秒,可以double(end -begin)*1000/CLOCKS_PER_SEC

一般来说,只要求精确到秒的话,time是很好使的
  1. #include <</span>stdio.h>
  2. #include <</span>time.h>
  3.  
  4. int main(){
  5.     time_t t_startt_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系统下的多种实现方法,希望对大家有帮助。

Linux系统

使用gettimeofday接口:

下载:  gettimeofday.c
  1. #include <</span>stdio.h>
  2. #include <</span>sys/time.h>
  3.  
  4. int main() {
  5.     struct timeval startend;
  6.     gettimeofday( &startNULL );
  7.     sleep(3);
  8.     gettimeofday( &endNULL );
  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 <</span>stdio.h>
  2. #include <</span>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 <</span>windows.h>
  2. #include <</span>stdio.h>
  3.  
  4. int main() {
  5.     DWORD startstop;
  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 <</span>windows.h>
  2. #include <</span>stdio.h>
  3.  
  4. int main(){
  5.     LARGE_INTEGER li;
  6.     LONGLONG startendfreq;
  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 <</span>windows.h>
  2. #include <</span>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调用得到当前时间,计算时间差比较简单,根据年、月、日、时、分秒和毫秒计算出一个整数,再将两整数相减即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C#中,你可以使用不同的方法计算程序的运行时间。以下是三种常用的方法: 第一种方法是使用System.DateTime.Now方法。你可以在程序开始和结束时获取当前时间,并计算时间差来得到程序的运行时间。示例代码如下: DateTime dt1 = System.DateTime.Now; // 程序执行的代码 DateTime dt2 = System.DateTime.Now; TimeSpan ts = dt2.Subtract(dt1); Console.WriteLine("程序运行时间:{0} 毫秒", ts.TotalMilliseconds); 第二种方法是使用Stopwatch类。你需要创建一个Stopwatch对象,并在程序开始和结束时分别调用Start()和Stop()方法。然后,通过Elapsed属性来获取程序的运行时间。示例代码如下: System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); // 程序执行的代码 sw.Stop(); TimeSpan ts2 = sw.Elapsed; Console.WriteLine("程序运行时间:{0} 毫秒", ts2.TotalMilliseconds); 第三种方法是使用C API。你需要使用QueryPerformanceCounter函数来获取计数器的值,并使用QueryPerformanceFrequency函数获取计数器的频率。然后,通过计算差值来得到程序运行的时间。示例代码如下: long count1 = 0; long count2 = 0; long freq = 0; double result = 0; QueryPerformanceFrequency(ref freq); QueryPerformanceCounter(ref count1); // 程序执行的代码 QueryPerformanceCounter(ref count2); result = (double)(count2 - count1) / (double)freq; Console.WriteLine("程序运行时间:{0} 秒", result);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值