C++之程序时间统计类实现
2015-08-20 21:24
363 查看
/********** TimeCounter.h huangsy13@gmail.com **********/ #ifndef TIMECOUNTER #define TIMECOUNTER #include <iostream> #include <cstring> #include <ctime> #include <sstream> #include <cstdlib> #include "FileStruct.h" using namespace std; struct TimeCounter{ time_t beginT; time_t nowT; int totalTask; int completeTask; string lastUseT; void setUp(int totalTask0){ totalTask = totalTask0; nowT = 0; beginT = time(NULL); } bool update(int taskNow){ nowT = time(NULL); completeTask = taskNow; if (lastUseT == useTime() && completeTask < totalTask){ return false; } else{ lastUseT = useTime(); return true; } } string useTime(){ int r = nowT - beginT; return getTimeStr(r); } string restTime(){ if(completeTask == 0){ return "unknow"; } int restTT = double(totalTask-completeTask)*double(nowT-beginT)/double(completeTask); return getTimeStr(restTT); } string getTimeStr(int r){ int rs = r; int rh = rs/3600; int rm = rs/60; string str; if (rh != 0){ str = numToStr(rh) + " hours "; } if (rs >= 60){ str += numToStr(rm%60) + " minutes "; } str += numToStr(rs%60) + " seconds"; return str; } string percent(){ int p = 100*completeTask/totalTask; return numToStr(p)+"%"; } void print(bool clear){ if (clear) system("clear"); cout << "use time " << useTime()<<endl; cout << "rest Time "<< restTime() <<endl; cout << "complete " << percent() << endl; } void updateAndPrint(int nn,bool clear = true){ if(update(nn)){ print(clear); } } }; #endif /********* main.cpp huangsy13@gmail.com *********/ #include <iostream> #include <cstring> #include <ctime> #include <sstream> #include <cstdlib> #include "TimeCounter.h" using namespace std; int main(){ TimeCounter T; int totalTask = 5000; T.setUp(totalTask); int taskNow = 0; while(taskNow < totalTask){ T.updateAndPrint(++taskNow); for (int i = 0 ; i < 100000 ; i++){ int a = 50; int b = 100; while(a != b){ a++; } } } }
相关文章推荐
- 二叉树的C++实现代码
- C++那些细节--函数的默认参数
- C语言中do...while(0)的妙用-避免goto
- 推箱子
- C
- C++,当函数参数或者返回值是对象......(★firecat推荐★)
- C++ Primer 5e chapter 7
- Python实例浅谈之三Python与C/C++相互调用
- 用C语言编程求水仙花数
- Kuhn_Munkres最大权匹配算法C++模板
- C++知识点随笔(五):虚继承
- 用C语言编程实现大小写字母的转换
- C语言动态走迷宫
- 用C语言求N!
- 用C语言编程求三角形的面积
- 用C语言求一元二次方程的根
- 用C语言编程求青蛙王子问题
- C语言编程判断一个数是否为素数
- C语言编程求最小公倍数
- C++ 实现memcpy和strcpy