clock

计算调用进程使用的壁钟时间 。

clock_t clock( void );

返回值

自进程启动时,经过的运行时间 (以秒为单位CLOCKS_PER_SEC) 。 如果运行时间的计数不可用时,函数返回 -1,转换为 clock_t。

备注

clock 函数告知该调用进程的使用次数。 计时器的嘀声约等于 1/CLOCKS_PER_SEC 秒。

要求

例程

必需的标头

clock

<time.h>

有关其他兼容性信息,请参见“简介”中的兼容性

示例

// crt_clock.c
// This example prompts for how long
// the program is to run and then continuously
// displays the elapsed time for that period.
//

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

void sleep( clock_t wait );

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

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

   // Measure the duration of an event.
   printf( "Time to do %ld empty loops is ", i );
   start = clock();
   while( i-- ) 
      ;
   finish = clock();
   duration = (double)(finish - start) / CLOCKS_PER_SEC;
   printf( "%2.1f seconds\n", duration );
}

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

.NET Framework 等效项

不适用。若要调用标准 C 函数,请使用 PInvoke。有关更多信息,请参见平台调用示例

请参见

参考

时间管理

difftime、_difftime32、_difftime64

time、_time32、_time64