點擊(此處)摺疊或打開
-
// 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.
-
……
- ******************************************************************************/
-