# 标准库time.h函数clock() **函数原型** ```c clock_t clock(void); ``` **说明** 这个函数返回的是程序执行时的处理器时钟,其中 `clock_t` 其实是基本类型的别名,如果函数运行失败,则会返回-1值。在标准库time.h中有一个宏定义 `CLOCKS_PER_SEC` 这个就是CPU计算中定义的每秒的时钟脉冲频率,由硬件定义,当然在特定情况下可以重新定义,因为不同的CPU时钟脉冲频率都不同。 **实例** ```c /* clock example: frequency of primes */ #include /* printf */ #include /* clock_t, clock, CLOCKS_PER_SEC */ #include /* sqrt */ int frequency_of_primes (int n) { int i,j; int freq=n-1; for (i=2; i<=n; ++i) for (j=sqrt(i);j>1;--j) if (i%j==0) {--freq; break;} return freq; } int main () { clock_t t; int f; t = clock(); printf ("Calculating...\n"); f = frequency_of_primes (99999); printf ("The number of primes lower than 100,000 is: %d\n",f); t = clock() - t; printf ("It took me %d clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC); return 0; } ```