zoukankan      html  css  js  c++  java
  • c/c++计时函数

    c++计时函数


    function clock 

    clock_t clock(void) ;

    Returns the processor time consumed by the program.

    The value returned is expressed in clock ticks, which are units of time of a constant but system-specific length (with a relation of CLOCKS_PER_SEC clock ticks per second).

    The epoch used as reference by clock varies between systems, but it is related to the program execution (generally its launch). To calculate the actual processing time of a program, the value returned by clock shall be compared to a value returned by a previous call to the same function.

    查阅资料:http://www.cplusplus.com/reference/ctime/clock/?kw=clock


    该函数是返回从程序进程开始调用clock()函数之间的CPU时钟计时单元(clock tick)数.返回类型clock_t为long.在time.h文件中存在定义:

    #ifndef _CLOCK_T_DEFINED
    typedef long clock_t;
    #define _CLOCK_T_DEFINED
    #endif

    同时存在
    #define CLOCKS_PER_SEC ((clock_t)1000)

    该语句表示定义了一个常量CLOCKS_PER_SEC,用来表示一秒钟会有多少个时钟计时单元,所以用clock()获取程序执行期间的clock tick总数再除以CLOCKS_PER_SEC可以获得程序执行时间(单位:秒)

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include<time.h>
     5 #define n 201
     6 
     7 int a[n],b[n],c[n*n]={0},lena,lenb;
     8 char reversea[n],reverseb[n];
     9 int t;
    10 clock_t start,finish;
    11 double TotTime;
    12 
    13 void Reverse()
    14 {
    15     int i;
    16     for (i=1;i<=lena;i++) a[i]=reversea[lena-i]-48;
    17     for (i=1;i<=lena;i++) b[i]=reverseb[lenb-i]-48;
    18     return;
    19 }
    20 
    21 void Multiply()
    22 {
    23     int result,res,i,j;
    24     for (i=1;i<=lenb;i++)
    25     {
    26         res=0;
    27         for (j=1;j<=lena;j++)
    28         {
    29             t=i+j;
    30             result=a[j]*b[i]+res+c[t-1];
    31             res=result/10;
    32             c[t-1]=result%10;
    33         }
    34         while(res)
    35         {
    36             c[t++]=res%10;
    37             res/=10;
    38         }
    39     }
    40     while (c[t]==0) t--;
    41     return;
    42 }
    43 
    44 void print(int c[])
    45 {
    46     int i;
    47     for (i=t;i>=1;i--) printf("%d",c[i]);
    48     printf("
    ");
    49     printf("The program cost %f seconds.
    ",TotTime); 
    50     return;
    51 }
    52 
    53 int main(int argc, char *argv[]) {
    54     start = clock();
    55     gets(reversea);
    56     gets(reverseb);
    57     lena=strlen(reversea);
    58     lenb=strlen(reverseb);
    59     Reverse();
    60     Multiply();
    61     finish = clock();
    62     printf("start = %d,finish = %d
    ",start,finish);
    63     TotTime = (double)(finish-start)/CLOCKS_PER_SEC; 
    64     print(c);
    65     
    66     return 0;
    67 }

    用了个别人写的c语言大数乘法运算,模拟的手写计算,但是单是计算过程的话变量start和finish获取的值是一样的,测不出运行时间...得把输入或者输出括进去...

    不过估计用次数比较多的循环和较大计算量的程序应该是能测出来的√

    参考博文:http://crane.is-programmer.com/posts/8080.html

  • 相关阅读:
    Spring3整合Quartz实现定时作业
    伪静态URLRewrite学习笔记
    VC 获取系统特殊文件夹的路径如:系统目录,桌面等
    正反向代理
    过虚拟机检测
    PDB符号文件信息
    Win64 驱动内核编程-33.枚举与删除对象回调
    Windows 反调试技术——OpenProcess 权限过滤
    Win10如何开启蓝屏记录?Win10开启蓝屏信息记录的方法
    Win7 x64下进程保护与文件保护(ObRegisterCallbacks)
  • 原文地址:https://www.cnblogs.com/h404nofound/p/14044552.html
Copyright © 2011-2022 走看看