我想通过查看算法的运行时性能来测试哪种数据结构是最好的,我该怎么做?
例如我已经有一个hashmap<string, int> hmp
;假设我有"apple"
in my hashmap
我想知道以下语句执行需要多长时间:hmp["apple"]
.
我怎样才能计时?
Thanks!
首先看一下我的回复这个问题 https://stackoverflow.com/questions/1861294/how-to-calculate-execution-time-of-a-code-snippet-in-c/1861337#1861337;它包含一个可移植(windows/linux)函数来获取以毫秒为单位的时间。
接下来,做这样的事情:
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;
全做完了! (请注意,我没有尝试编译它)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)