我要测试的数据结构是最好的看着我的算法的运行时性能,我怎么办呢?
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"].
我怎样才能几点了?
谢谢!
推荐答案首先来看看我的回答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)