问题描述
确定处理例程(例如函数过程)所需时间的最佳和最准确方法是什么?
What would be the best and most accurate way to determine how long it took to process a routine, such as a procedure of function?
我之所以这么问是因为我目前正在尝试优化我的应用程序中的一些功能,当我测试更改时,很难仅通过查看它来确定是否有任何改进.因此,如果我能够返回处理例程所需的准确或接近准确的时间,那么我就会更清楚地了解代码是否进行了任何更改.
I ask because I am currently trying to optimize a few functions in my Application, when i test the changes it is hard to determine just by looking at it if there was any improvements at all. So if I could return an accurate or near accurate time it took to process a routine, I then have a more clear idea of how well, if any changes to the code have been made.
我考虑过使用 GetTickCount,但我不确定这是否接近准确?
I considered using GetTickCount, but I am unsure if this would be anything near accurate?
有一个可重用的函数/过程来计算例程的时间会很有用,并像这样使用它:
It would be useful to have a resuable function/procedure to calculate the time of a routine, and use it something like this:
// < prepare for calcuation of code
...
ExecuteSomeCode; // < code to test
...
// < stop calcuating code and return time it took to process
我期待听到一些建议.
谢谢.
克雷格.
推荐答案
据我所知,最准确的方法是使用 QueryPerformanceFrequency:
From my knowledge, the most accurate method is by using QueryPerformanceFrequency:
代码:
var
Freq, StartCount, StopCount: Int64;
TimingSeconds: real;
begin
QueryPerformanceFrequency(Freq);
QueryPerformanceCounter(StartCount);
// Execute process that you want to time: ...
QueryPerformanceCounter(StopCount);
TimingSeconds := (StopCount - StartCount) / Freq;
// Display timing: ...
end;
这篇关于计算例程的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!