difftime64()- 计算时间差异

格式

#include <time.h>
double difftime64(time64_t time2, time64_t time1);

语言级别

ILE C 扩展

线程安全

描述

difftime64() 函数计算 time2time1之间的差值 (以秒计)。

返回值

difftime64() 函数以双精度数形式返回从 time1time2 的耗用时间 (以秒为单位)。 类型 time64_t<time.h>中定义。

示例

此示例显示了使用 difftime64()的计时应用程序。 此示例计算查找从 2 到 10 000 的素数所花费的平均时间。
#include <time.h>
#include <stdio.h>
 
#define RUNS 1000
#define SIZE 10000
 
int mark[SIZE];
 
int main(void)
{
   time64_t start, finish;
   int i, loop, n, num;
 
   time64(&start);
 
   /*  This loop finds the prime numbers between 2 and SIZE   */
   for (loop = 0; loop < RUNS; ++loop)
      {
      for (n = 0; n < SIZE; ++n)
         mark [n] = 0;
      /*  This loops marks all the composite numbers with -1  */
      for (num = 0, n = 2; n < SIZE; ++n)
         if ( ! mark[n])
         {
            for (i = 2 * n; i < SIZE; i += n)
                mark[i] = -1;
            ++num;
         }
      }
   time64(&finish);
   printf("Program takes an average of %f seconds "
                  "to find %d primes.\n",
                   difftime64(finish,start)/RUNS, num);
}
 
/********************  Output should be similar:  *****************
 
The program takes an average of 0.106000 seconds to find 1229 primes.
*/

相关信息