Chinaunix首页 | 论坛 | 博客
  • 博客访问: 508730
  • 博文数量: 23
  • 博客积分: 398
  • 博客等级: 一等列兵
  • 技术积分: 850
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-03 22:18
文章分类

全部博文(23)

文章存档

2013年(9)

2012年(14)

分类: C/C++

2012-10-12 19:10:56

   之前,我的同学问了我一个算法题,由于这个题是要通过提交代码然后在线测试的,有运行时间的限制。我想应该有办法把某段代码的运行时间计算出来,当然现在某些IDE(集成开发环境)已经提供了这个功能,但是我猜它只是计算进程开始至结束的时间,如果我们需要更精确,精确到某段代码的运行时间的话,我们可以在代码中加入相应的代码就可以得到这个运行时间了。很多时候可以用于比较两(几)个算法的效率。
 
   下面给出两个比较方便的方法在C语言中的使用(C++的更多本文不讨论)

点击(此处)折叠或打开

  1. // count_time.c

  2. // 求2000000内素数和,并计算所用时间(精确)
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <sys/time.h> // struct timeval 的定义
  6. #include <time.h> // clock的定义

  7. void prime_number()
  8. {
  9.     unsigned long m, k, i;//, n = 0;
  10.     unsigned long sum = 0;

  11.     printf("2000000内素数和为:\n");
  12.     for (m = 3; m < 2000000;m += 2) {
  13.         k = (unsigned long)sqrt(m);
  14.         for (i = 2; i <= k; i++) {
  15.             if (m % i == 0)
  16.                 break;
  17.         }
  18.         if (i >= k + 1)
  19.             sum += m;
  20.     }
  21.     printf("sum= %lld\n",sum+2);
  22. }

  23. // 计时方法一( 在Unix/Linux下使用)
  24. void count_runtime1()
  25. {
  26.     struct timeval start, end;

  27.     gettimeofday( &start, NULL );//第二个参数不为空则用于返回时区结果,这里不需要

  28.     //* 在start与end之间运行需要计时的代码
  29.     prime_number();
  30.     
  31.     gettimeofday( &end, NULL );
  32.     
  33.     unsigned int timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + \
  34.                 end.tv_usec -start.tv_usec;
  35.                         
  36.     printf("timeuse: %d us(微妙)\n", timeuse);
  37. }

  38. // 计时方法2
  39. void count_runtime2()
  40. {
  41.     
  42.     /* 使用下面的clock获取,更接近cpu运行时间 ,标准C更通用*/
  43.     clock_t clock_start = clock();
  44.     
  45.     prime_number();// 需要计时的代码
  46.     
  47.     clock_t clock_end = clock();
  48.     
  49.     printf("timesue: %ld us\n", clock_end - clock_start);
  50. }

  51. int main()
  52. {
  53.     count_runtime1();
  54.     count_runtime2();
  55.     
  56.     return 0;
  57. }
  58. /******************************************************************************/
  59. /* man 手册中对gettimeofday函数的说明:
  60. NAME
  61.        gettimeofday, settimeofday - get / set time

  62. SYNOPSIS
  63.        #include <sys/time.h>

  64.        int gettimeofday(struct timeval *tv, struct timezone *tz);
  65.        int settimeofday(const struct timeval *tv, const struct timezone *tz);

  66.    Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

  67.        settimeofday(): _BSD_SOURCE

  68. DESCRIPTION
  69.        The functions gettimeofday() and settimeofday() can get and set      the time as well as a timezone. The tv argument is a struct          meval (as specified in <sys/time.h>):

  70.            struct timeval {
  71.                time_t tv_sec; // seconds
  72.                suseconds_t tv_usec; // microseconds
  73.            };
  74.   …………
  75.   RETURN VALUE
  76.        gettimeofday() and settimeofday() return 0 for success, or -1 for fail?br />
  77.        ure (in which case errno is set appropriately).

  78. ***************************************************************************************/

  79. /* man 手册中对clock函数的说明:
  80. NAME
  81.        clock - Determine processor time

  82. SYNOPSIS
  83.        #include <time.h>

  84.        clock_t clock(void);

  85. DESCRIPTION
  86.        The clock() function returns an approximation of processor time      used by the program.

  87. RETURN VALUE
  88.        The value returned is the CPU time used so far as a clock_t; to get      the number of seconds used, divide by CLOCKS_PER_SEC. If the      processor time used is not available or its value cannot be      represented, the
  89.        function returns the value (clock_t) -1.

  90. CONFORMING TO
  91.        C89, C99, POSIX.1-2001. POSIX requires that CLOCKS_PER_SEC equals
  92.        1000000 independent of the actual resolution.
  93.        int /usr/include/time.h #define CLOCKS_PER_SEC 1000000l

  94. NOTES
  95.        The C standard allows for arbitrary values at the start of the program;subtract the value returned from a call to clock() at the      start of the program to get maximum portability.
  96. ……
  97. ******************************************************************************/

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