请教:如何求一段程序的运行时间(菜)。
我要得到一段程序的运行时间,用的是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




