Share via


clock

更新 : 2007 年 11 月

呼び出しプロセスにかかったウォール クロック時間を計算します。

clock_t clock( void );

戻り値

プロセス開始時からのウォール クロックの経過時間 (秒単位の経過時間 CLOCKS_PER_SEC) を返します。経過時間を取得できない場合は、clock_t にキャストした –1 を返します。

解説

clock 関数は、呼び出しプロセスにかかった時間を通知します。タイマ刻みの単位は、約 1/CLOCKS_PER_SEC 秒です。Microsoft C 6.0 より前のバージョンでは、CLOCKS_PER_SEC 定数は CLK_TCK と呼ばれていました。

必要条件

ルーチン

必須ヘッダー

clock

<time.h>

互換性の詳細については、「C ランタイム ライブラリ」の「互換性」を参照してください。

使用例

// 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() )
      ;
}

Delay for three seconds
Done!
Time to do 6000000 empty loops is 0.1 seconds

.NET Framework の相当するアイテム

適用できません。標準 C 関数を呼び出すには、PInvoke を使用します。詳細については、「プラットフォーム呼び出しの例」を参照してください。

参照

参照

時間管理

difftime、_difftime32、_difftime64

time、_time32、_time64