之前,我的同学问了我一个算法题,由于这个题是要通过提交代码然后在线测试的,有运行时间的限制。我想应该有办法把某段代码的运行时间计算出来,当然现在某些IDE(集成开发环境)已经提供了这个功能,但是我猜它只是计算进程开始至结束的时间,如果我们需要更精确,精确到某段代码的运行时间的话,我们可以在代码中加入相应的代码就可以得到这个运行时间了。很多时候可以用于比较两(几)个算法的效率。
下面给出两个比较方便的方法在C语言中的使用(C++的更多本文不讨论)
- // count_time.c
- // 求2000000内素数和,并计算所用时间(精确)
- #include <stdio.h>
- #include <math.h>
- #include <sys/time.h> // struct timeval 的定义
- #include <time.h> // clock的定义
- void prime_number()
- {
- unsigned long m, k, i;//, n = 0;
- unsigned long sum = 0;
- printf("2000000内素数和为:\n");
- for (m = 3; m < 2000000;m += 2) {
- k = (unsigned long)sqrt(m);
- for (i = 2; i <= k; i++) {
- if (m % i == 0)
- break;
- }
- if (i >= k + 1)
- sum += m;
- }
- printf("sum= %lld\n",sum+2);
- }
- // 计时方法一( 在Unix/Linux下使用)
- void count_runtime1()
- {
- struct timeval start, end;
- gettimeofday( &start, NULL );//第二个参数不为空则用于返回时区结果,这里不需要
- //* 在start与end之间运行需要计时的代码
- prime_number();
-
- gettimeofday( &end, NULL );
-
- unsigned int timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + \
- end.tv_usec -start.tv_usec;
-
- printf("timeuse: %d us(微妙)\n", timeuse);
- }
- // 计时方法2
- void count_runtime2()
- {
-
- /* 使用下面的clock获取,更接近cpu运行时间 ,标准C更通用*/
- clock_t clock_start = clock();
-
- prime_number();// 需要计时的代码
-
- clock_t clock_end = clock();
-
- printf("timesue: %ld us\n", clock_end - clock_start);
- }
- int main()
- {
- count_runtime1();
- count_runtime2();
-
- return 0;
- }
- /******************************************************************************/
- /* man 手册中对gettimeofday函数的说明:
- NAME
- gettimeofday, settimeofday - get / set time
- SYNOPSIS
- #include <sys/time.h>
- int gettimeofday(struct timeval *tv, struct timezone *tz);
- int settimeofday(const struct timeval *tv, const struct timezone *tz);
- Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
- settimeofday(): _BSD_SOURCE
- DESCRIPTION
- 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>):
- struct timeval {
- time_t tv_sec; // seconds
- suseconds_t tv_usec; // microseconds
- };
- …………
- RETURN VALUE
- gettimeofday() and settimeofday() return 0 for success, or -1 for fail?br />
- ure (in which case errno is set appropriately).
- ***************************************************************************************/
- /* man 手册中对clock函数的说明:
- NAME
- clock - Determine processor time
- SYNOPSIS
- #include <time.h>
- clock_t clock(void);
- DESCRIPTION
- The clock() function returns an approximation of processor time used by the program.
- RETURN VALUE
- 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
- function returns the value (clock_t) -1.
- CONFORMING TO
- C89, C99, POSIX.1-2001. POSIX requires that CLOCKS_PER_SEC equals
- 1000000 independent of the actual resolution.
- int /usr/include/time.h #define CLOCKS_PER_SEC 1000000l
- NOTES
- 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.
- ……
- ******************************************************************************/
阅读(3019) | 评论(0) | 转发(0) |