声明并实现一个测试代码执行时间的类-口天草芳

//
//文件名:  testclk.h
//作者:    口天草芳
//编写日期:2002.9.18
//编写目的:声明并实现一个测试代码执行时间的类
//

#ifndef TESTCLK_H
#define TESTCLK_H

#include <time.h>

const int CLOCK_SEC=1000; 
class Testclk
{
    double timestart; //定义程序开始时间
    double timeend; //定义程序结束时间
    double protime; //进程的执行时间
 public:
    Testclk();       
    double ProcessStart(); //进程开始计时
    double ProcessEnd();   //进程结束计时
    double ShowTime();     //显示进程执行时间

   ~Testclk();
};

//下面的代码为成员函数的实现

Testclk::Testclk()
{
  timestart=0.0;
  timeend=0.0;
  protime=0.0;
}

double Testclk::ProcessStart()
{
  cout<<"开始计时:"<<endl;
  timestart=(double)clock()/(double)

CLOCK_SEC;
  return timestart;
}

double Testclk::ProcessEnd()
{
  cout<<"计时结束: "<<endl;
  timeend=(double)clock()/(double)

CLOCK_SEC;
  protime=timeend-timestart;
  return protime;
}
 
double Testclk::ShowTime()
{
  cout<<"进程执行时间为:"<<protime<<endl;

}

Testclk::~Testclk()
{
  cout<<"测试结束:-D"<<endl;
}

#endif
 
 
测试代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "testclk.h"


int main(int argc, char *argv[])
{
   Testclk testob;
   testob.ProcessStart();
   cout<<"程序正在执行..."<<endl;
   Sleep(2000);
   cout<<"程序完成!!"<<endl;
   testob.ProcessEnd();
   testob.ShowTime();

      system("PAUSE");    
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值