嘿伙计们,我正在尝试以微秒为单位编写一些搜索功能,它需要足够长的时间才能显示 2 个有效数字。我写了这段代码来计时我的搜索功能,但它似乎太快了。我总是最终得到 0 微秒,除非我运行搜索 5 次然后我得到 1,000,000 微秒。我想知道我是否做错了数学以获取以微秒为单位的时间,或者是否有某种格式化功能可以用来强制它显示两个 sig figs?
clock_t start = clock();
index = sequentialSearch.Sequential(TO_SEARCH);
index = sequentialSearch.Sequential(TO_SEARCH);
clock_t stop = clock();
cout << "number found at index " << index << endl;
int time = (stop - start)/CLOCKS_PER_SEC;
time = time * SEC_TO_MICRO;
cout << "time to search = " << time<< endl;