clock

计算调用进程所用的时钟时间。

语法

clock_t clock( void );

返回值

自进程开始 CRT 初始化后的运行时间,以每秒 CLOCKS_PER_SEC 单位数进行测量。 如果运行时间不可用或已超出可记录为 clock_t 类型的最大正时间,该函数将返回值 (clock_t)(-1)

备注

clock 函数指示在进程开始期间自 CRT 初始化以来所经过的时钟时间。 此函数并不严格遵守 ISO C,它将净 CPU 时间指定为返回值。 若要获取 CPU 时间,请使用 Win32 GetProcessTimes 函数。 若要确定运行时间(以秒为单位),将 clock 函数返回的值除以宏 CLOCKS_PER_SEC

只要有足够的时间,clock 返回的值就可能超过 clock_t 的最大正值。 当进程运行更长时间后,clock 返回的值始终为 (clock_t)(-1),如 ISO C99 标准 (7.23.2.1) 和 ISO C11 标准 (7.27.2.1) 中所指定的那样。 Microsoft 将 clock_t 实现为 long,这是一个带符号的 32 位整数,将 CLOCKS_PER_SEC 宏定义为 1000。 此宏使 clock 函数的最大返回值为 2147483.647 秒(或大约 24.8 天)。 不依赖于运行时间超过此时间的进程中 clock 返回的值。 可以使用 64 位 time 函数或 Windows QueryPerformanceCounter 函数来记录多年进程运行时间。

要求

例程 必需的标头
clock <time.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_clock.c
// This sample uses clock() to 'sleep' for three
// seconds, then determines how long it takes
// to execute an empty loop 600000000 times.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Pauses for a specified number of clock cycles.
void do_sleep( clock_t wait )
{
   clock_t goal;
   goal = wait + clock();
   while( goal > clock() )
      ;
}

const long num_loops = 600000000L;

int main( void )
{
   long    i = num_loops;
   clock_t start, finish;
   double  duration;

   // Delay for a specified time.
   printf( "Delay for three seconds\n" );
   do_sleep( (clock_t)3 * CLOCKS_PER_SEC );
   printf( "Done!\n" );

   // Measure the duration of an event.
   start = clock();
   while( i-- )
      ;
   finish = clock();
   duration = (double)(finish - start) / CLOCKS_PER_SEC;
   printf( "Time to do %ld empty loops is ", num_loops );
   printf( "%2.3f seconds\n", duration );
}
Delay for three seconds
Done!
Time to do 600000000 empty loops is 1.354 seconds

另请参阅

工时管理
difftime_difftime32_difftime64
time_time32_time64