c语言clock函数使用示例_C 语言

clock_t clock( void );
Calculates the processor time used by the calling process
head file is <time.h>

Return Value
clock returns the number of clock ticks of elapsed processor time. The returned value
is the product of the amount of time that has elapsed since the start of a process and
the value of the CLOCKS_PER_SEC constant. If the amount of elapsed time is
unavailable, the function returns –1, cast as a clock_t.

Remarks
The clock function tells how much processor time the calling process has used. The
time in seconds is approximated by dividing the clock return value by the value of the
CLOCKS_PER_SEC constant. In other words, clock returns the number of
processor timer ticks that have elapsed. A timer tick is approximately equal to
1/CLOCKS_PER_SEC second. In versions of Microsoft C before 6.0, the
CLOCKS_PER_SEC constant was called CLK_TCK.

复制代码 代码如下:

/* 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 );
void main( void )
{
   long    i = 600000000L;
   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() )

}//sleep

时间: 2024-11-10 10:10:42

c语言clock函数使用示例_C 语言的相关文章

c++回调之利用函数指针示例_C 语言

c++回调之利用函数指针示例 复制代码 代码如下: #include <iostream>using namespace std; /************************************************************************//*                下层实现: CALLBACK                                        *//**********************************

浅谈Linux环境下并发编程中C语言fork()函数的使用_C 语言

由fork创建的新进程被称为子进程(child process).fork函数被调用一次,但返回两次.子进程的返回值是0,而父进程的返回值则是新进程的进程ID.将子进程ID返回给父进程的理由是:因为一个进程的子进程可以有多个,并且没有一个函数使一个进程可以获得其所有子进程的进程ID.fork使子进程得到返回值0的理由是:一个进程只会有一个父进程,所以子进程总是可以调用getpid以获得其父进程的进程ID. 使fork失败的两个主要原因是:系统中已经有了太多的进程,或者该实际用户ID的进程总数超过

C语言fillpoly函数详解_C 语言

C语言中,fillpoly函数的功能是画一个多边形,今天我们就来学习学习. C语言fillpoly函数:填充一个多边形 函数名:fillpoly 功  能:画并填充一个多边形 头文件:#include <graphics.h> 原  型:fillpoly(int numpoints, int far *polypoints); 参数说明:numpoints 为多边形的边数:far *polypoints 为存储各顶点坐标的数组,每两个一组表示一个顶点的 X 和 Y 坐标. 实例代码: #inc

C语言socket编程开发应用示例_C 语言

实现步骤: 1. Server端 复制代码 代码如下: #include <stdio.h>                   //用于printf等函数的调用#include <winsock2.h>                //Socket的函数调用 #pragma comment (lib, "ws2_32")      //C语言引用其他类库时,除了.h文件外,还要加入对应的lib文件(这个不同于C#) 复制代码 代码如下: int main()

c语言strftime时间格式化示例_C 语言

函数原型: 复制代码 代码如下: size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr ); 代码示例: 复制代码 代码如下: #include <stdio.h>#include <time.h> int main (){    time_t rawtime;    struct tm * timeinfo;    char buffer [128];    

利用c语言实现卷积码编码器示例_C 语言

实现(2, 1, 7)卷积码编码信息序列1001 1010 1111 1100生成序列g1 = 1011011;g2 = 1111001初始状态全0.以上参数可自行在main中修改. 复制代码 代码如下: /***This is an simple example program of convolutional encoder.   *The information sequence, the register initial states and the generation sequenc

c语言线程终止练习示例_C 语言

复制代码 代码如下: #include <stdio.h>#include <stdlib.h>#include <pthread.h> void *t1(void *args) { return (void *) 0;} void *t2(void *args) { printf("thread 2 param[args] = %d\n", args); pthread_exit((void *) 3);} void *t3(void *args)

c语言求阶乘精确值示例_C 语言

复制代码 代码如下: #include <stdio.h>#include <string.h>const int maxn = 3000;int f[maxn];int main(){ int i,j,n; scanf("%d",&n); memset(f,0,sizeof(f)); f[0] = 1; for(i = 2;i <= n;i++) {  int c = 0;  for(j = 0;j < maxn;j++)  {   int

c语言多进程tcp服务器示例_C 语言

server.h 复制代码 代码如下: #ifndef SERVER_H#define SERVER_H#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <arpa/inet.h>#include <as