VC/MFC中计算程序/系统运行时间

素颜马尾好姑娘i 2022-09-26 12:00 286阅读 0赞

法一 利用GetTickCount函数

获取程序运行时间

复制代码

  1. 。。。
  2. long
  3. t1
  4. =
  5. GetTickCount();
  6. //
  7. 程序段开始前取得系统运行时间(ms)
  8. 。。。。。。
  9. //
  10. to do sth
  11. long
  12. t2
  13. =
  14. GetTickCount();
  15. //
  16. 程序段结束后取得系统运行时间(ms)
  17. cout
  18. <<
  19. t2
  20. -
  21. t1
  22. <<
  23. endl;
  24. //
  25. 前后之差即程序运行时间
  26. 。。。

复制代码

获取系统运行时间

ExpandedBlockStart.gif

复制代码

  1. CString str;
  2. //
  3. 获取程序运行时间
  4. long
  5. t1
  6. =
  7. GetTickCount();
  8. //
  9. 程序段开始前取得系统运行时间(ms)
  10. //
  11. Sleep(500);
  12. //
  13. AfxMessageBox("do something...");
  14. 。。。。。。
  15. //
  16. to do sth
  17. long
  18. t2
  19. =
  20. GetTickCount();
  21. //
  22. 程序段结束后取得系统运行时间(ms)
  23. str.Format(
  24. "
  25. time:%dms
  26. "
  27. ,t2
  28. -
  29. t1);
  30. //
  31. 前后之差即程序运行时间
  32. AfxMessageBox(str);

复制代码

法二 利用C/C++计时函数

获取程序运行时间

ExpandedBlockStart.gif

复制代码

  1. #include
  2. "
  3. time.h
  4. "
  5. 。。。
  6. clock_t start, finish;
  7. start
  8. =
  9. clock();
  10. 。。。。。。
  11. //
  12. to do sth
  13. finish
  14. =
  15. clock();
  16. //
  17. cout<<(double)(finish-start)/CLOCKS_PER_SEC<<" seconds"<<endl;
  18. printf(
  19. "
  20. %f seconds\n
  21. "
  22. ,(
  23. double
  24. )(finish
  25. -
  26. start)
  27. /
  28. CLOCKS_PER_SEC);
  29. 。。。

复制代码

函数/参数说明
















clock()

C/C++计时函数,与其相关的数据类型是clock_t

返回:从”此程序进程开启”到”程序中调用clock()函数”之间CPU计时单元数,MSDN中称挂钟时间(wal-clock)

clock_t

用来保存时间的数据类型,在time.h中定义:typedef   long   clock_t;  为长整型

CLOCKS_PER_SEC

用来表示一秒钟会有多少个时钟计时单元,在time.h中定义:#define CLOCKS_PER_SEC ((clock_t)1000)  

获取系统运行的时间

ExpandedBlockStart.gif

复制代码

  1. CString str,str1;
  2. //
  3. 获取系统运行时间
  4. long
  5. t
  6. =
  7. GetTickCount();
  8. str1.Format(
  9. "
  10. 系统已运行 %d时
  11. "
  12. ,t
  13. /
  14. 3600000
  15. );
  16. str
  17. =
  18. str1;
  19. t
  20. %=
  21. 3600000
  22. ;
  23. str1.Format(
  24. "
  25. %d分
  26. "
  27. ,t
  28. /
  29. 60000
  30. );
  31. str
  32. +=
  33. str1;
  34. t
  35. %=
  36. 60000
  37. ;
  38. str1.Format(
  39. "
  40. %d秒
  41. "
  42. ,t
  43. /
  44. 1000
  45. );
  46. str
  47. +=
  48. str1;
  49. AfxMessageBox(str);

复制代码

法三 利用CTime类 获取系统时间

复制代码

  1. CString str;
  2. //
  3. 获取系统时间
  4. CTime tm;
  5. tm
  6. =
  7. CTime::GetCurrentTime();
  8. str
  9. =
  10. tm.Format(
  11. "
  12. 现在时间是%Y年%m月%d日 %X
  13. "
  14. );
  15. AfxMessageBox(str);

复制代码

法四 利用GetLocalTime类 获取系统时间

ExpandedBlockStart.gif

复制代码

  1. SYSTEMTIME st;
  2. CString strDate,strTime;
  3. GetLocalTime(
  4. &
  5. st);
  6. strDate.Format(
  7. "
  8. %4d-%2d-%2d
  9. "
  10. ,st.wYear,st.wMonth,st.wDay);
  11. strTime.Format(
  12. "
  13. %2d:%2d:%2d
  14. "
  15. ,st.wHour,st.wMinute,st.wSecond);
  16. AfxMessageBox(strDate);
  17. AfxMessageBox(strTime);

发表评论

表情:
评论列表 (有 0 条评论,286人围观)

还没有评论,来说两句吧...

相关阅读

    相关 计算程序运行时间

    在编写完程序后,通常都会对程序进行性能测试,比较常用的方法就是计算完成某个任务所花费的时间。System类提供了获得当前时间的方法,但是其单位是毫秒,阅读不方便。本实例将其转换