如何知道计算在C算法的执行时间++? [英] How to know calculate the execution time of an algorithm in c++?
问题描述
我要测试的数据结构是最好的看着我的算法的运行时性能,我怎么办呢?
I want to test which data structure is the best by looking at the run-time performance of my algorithm, how do I do it?
例如我已经有一个的HashMap<字符串,INT> HMP
;假设我有苹果
在我的的HashMap
我想知道多久下面的语句执行的时间:麝香保心丸【苹果]
。
For example I already have a hashmap<string, int> hmp
; assuming I have "apple"
in my hashmap
I want to know how long the following statement takes to execute: hmp["apple"]
.
我怎样才能几点了?
谢谢!
推荐答案
首先来看看我的回答<一href="http://stackoverflow.com/questions/1861294/how-to-calculate-execution-time-of-a-$c$c-snippet-in-c/1861337#1861337">this问题;它包含了一个便携式(的Windows / Linux)函数来获得以毫秒为单位的时间。
First of all take a look at my reply to this question; it contains a portable (windows/linux) function to get the time in milliseconds.
接下来,做这样的事情:
Next, do something like this:
int64 start_time = GetTimeMs64();
const int NUM_TIMES = 100000; /* Choose this so it takes at the very least half a minute to run */
for (int i = 0; i < NUM_TIMES; ++i) {
/* Code you want to time.. */
}
double milliseconds = (GetTimeMs64() - start_time) / (double)NUM_TIMES;
全部完成! (请注意,我没有试图编译)
All done! (Note that I haven't tried to compile it)
这篇关于如何知道计算在C算法的执行时间++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!