CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

请教:如何求一段程序的运行时间(菜)。

楼主klmswt(塞北的雪)2002-06-07 11:23:07 在 VC/MFC / 基础类 提问

我要得到一段程序的运行时间,用的是GetTickCount(),它是得出从开机到当前的时间  
   
  在程序开始       time1=GetTickCount();  
  程序结束时       time2=GetTickCount();  
  本段程序运行时间=time2-time1;  
  但问什么我总得到结果0?  
  问题点数:20、回复次数:13Top

1 楼masterz(www.fruitfruit.com)回复于 2002-06-07 14:25:56 得分 8

__int64     lfreq;  
  BOOL     bret=QueryPerformanceFrequency((LARGE_INTEGER*)&lfreq);  
   
  __int64     lstart;  
  QueryPerformanceCounter((LARGE_INTEGER*)&lstart);  
   
  ...  
  __int64     lstop;  
  QueryPerformanceCounter((LARGE_INTEGER*)&lstop);  
   
  __int64   lruntime=lstop   -   lstart;  
  double   dsec=   lruntime/lfreq;Top

2 楼masterz(www.fruitfruit.com)回复于 2002-06-07 14:26:49 得分 0

double   dsec=   ((double)lruntime)/((double)lfreq);Top

3 楼klmswt(塞北的雪)回复于 2002-06-07 14:34:46 得分 0

我听说QueryPerformenceFrequency()  
  和QueryPerformenceCounter()只能在win9x下使用,我的系统是win2000  
  可以用它们吗?  
  Top

4 楼whz_time(TimesWU)回复于 2002-06-07 14:50:16 得分 2

不要time1,time2就是程序运行的时间Top

5 楼klmswt(塞北的雪)回复于 2002-06-07 14:54:33 得分 0

(雨一直下):  
  time2是所有的运行时间,我需要的是中间一段程序的时间.  
  Top

6 楼whz_time(TimesWU)回复于 2002-06-07 14:58:47 得分 0

我测试过,象你那样是没问题的!  
  Top

7 楼kiko_lee(清醒的迷茫中)回复于 2002-06-07 15:02:34 得分 5

QueryPerformanceFrequency  
  The   QueryPerformanceFrequency   function   retrieves   the   frequency   of   the   high-resolution   performance   counter,   if   one   exists.   The   frequency   cannot   change   while   the   system   is   running.  
   
  BOOL   QueryPerformanceFrequency(  
      LARGE_INTEGER   *lpFrequency       //   current   frequency  
  );  
  Parameters  
  lpFrequency    
  [out]   Pointer   to   a   variable   that   receives   the   current   performance-counter   frequency,   in   counts   per   second.   If   the   installed   hardware   does   not   support   a   high-resolution   performance   counter,   this   parameter   can   be   zero.    
  Return   Values  
  If   the   installed   hardware   supports   a   high-resolution   performance   counter,   the   return   value   is   nonzero.  
   
  If   the   function   fails,   the   return   value   is   zero.   To   get   extended   error   information,   call   GetLastError.   For   example,   if   the   installed   hardware   does   not   support   a   high-resolution   performance   counter,   the   function   fails.    
   
  Requirements    
      Windows   NT/2000:   Requires   Windows   NT   3.1   or   later.  
      Windows   95/98:   Requires   Windows   95   or   later.  
      Header:   Declared   in   Winbase.h;   include   Windows.h.  
      Library:   Use   Kernel32.lib.  
   
  See   Also  
  Timers   Overview,   Timer   Functions,   QueryPerformanceCounter    
   
  QueryPerformanceFrequency  
  The   QueryPerformanceFrequency   function   retrieves   the   frequency   of   the   high-resolution   performance   counter,   if   one   exists.   The   frequency   cannot   change   while   the   system   is   running.  
   
  BOOL   QueryPerformanceFrequency(  
      LARGE_INTEGER   *lpFrequency       //   current   frequency  
  );  
  Parameters  
  lpFrequency    
  [out]   Pointer   to   a   variable   that   receives   the   current   performance-counter   frequency,   in   counts   per   second.   If   the   installed   hardware   does   not   support   a   high-resolution   performance   counter,   this   parameter   can   be   zero.    
  Return   Values  
  If   the   installed   hardware   supports   a   high-resolution   performance   counter,   the   return   value   is   nonzero.  
   
  If   the   function   fails,   the   return   value   is   zero.   To   get   extended   error   information,   call   GetLastError.   For   example,   if   the   installed   hardware   does   not   support   a   high-resolution   performance   counter,   the   function   fails.    
   
  Requirements    
      Windows   NT/2000:   Requires   Windows   NT   3.1   or   later.  
      Windows   95/98:   Requires   Windows   95   or   later.  
      Header:   Declared   in   Winbase.h;   include   Windows.h.  
      Library:   Use   Kernel32.lib.  
   
  See   Also  
  Timers   Overview,   Timer   Functions,   QueryPerformanceCounter    
   
  自然可以用在2000下了   :)Top

8 楼jszj(老板说mis部不是赚钱的部门...)回复于 2002-06-07 15:03:33 得分 0

Declare   int   the   .h   file:  
        long   time;  
  Then   write   at   the   begin   of   program:  
        time=GetCurrentTime();  
  And   write   at   the   end   of   program:  
        time=GetCurrentTime()-time;  
   
  That's   all!Top

9 楼lydragon(神龙无悔)回复于 2002-06-07 15:08:48 得分 3

用GetTickCount是可以的,你總是得到零是因為你的程序運行時間太短了!  
  QueryPerfor...()系列函數在2000下也可以用的!Top

10 楼klmswt(塞北的雪)回复于 2002-06-07 15:19:19 得分 0

谢谢各位。  
  我试过queryPerformence...()了,结果不再为零了,  
  但是同样的数据几次运行的结果都不一样,请问这是什么原因,是因为误差吗?  
  Top

11 楼liugy(不知道的太多)回复于 2002-06-07 16:30:16 得分 2

系统不是实时响应的,分时操作系统的原因。你把进程优先级提到最高,看看是不是一样了?Top

12 楼klmswt(塞北的雪)回复于 2002-06-07 17:13:46 得分 0

我提高了优先级也是结果不一致。  
  Why??  
  Top

13 楼vcfor( )回复于 2002-07-02 19:55:03 得分 0

upTop

相关问题

  • 程序运行时间的测定
  • 如何得知程序运行时间?
  • 菜问题:dx程序运行出错
  • 程序长时间运行,其他程序不能连接oracle
  • 运行EVC编的读串口程序运行一段时间后点击PPC的开始菜单就会死机???
  • 如何在delph程序中判断程序运行了多少时间??
  • 请问运行一段c++程序,我如何看这段程序运行所花费的精确时间?
  • 怎样在程序运行中获得精确的时间?
  • 怎么防止同一程序同一时间运行两次?
  • 怎么显示程序的运行时间

关键词

  • hardware
  • lfreq
  • 运行
  • 程序
  • lruntime
  • lstop
  • lstart
  • queryperformancefrequency
  • frequency
  • gettickcount

得分解答快速导航

  • 帖主:klmswt
  • masterz
  • whz_time
  • kiko_lee
  • lydragon
  • liugy

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo