我正在寫一些某種形式和搜索算法和測試他們爲大學分配,我得的CPU時間和掛鐘時間在處理不同的測試中使用的量。以及個人時間。商店的boost ::計時器結果在一個變量
我使用升壓API來實現這一目標,我的問題是,我必須運行多個測試並拿到平均時間,但我找不到存儲結果升壓解決方案中的變量在我給。
這裏是我的算法之一:
int CA1::binarySearch(vector<int> v, int target)
{
boost::timer::auto_cpu_timer t("%w");
int top, bottom, middle;
top = vecSize - 1;
bottom = 0;
while (bottom <= top)
{
middle = (top + bottom)/2;
if (v[middle] == target)
return middle;
else if (v[middle] > target)
top = middle - 1;
else
bottom = middle + 1;
}
return -1;
}
編輯
@Surt心中已經想實現你的代碼如下:
int main()
{
int size = 0;
cout << " enter the size of your vector\n";
cin >> size;
CA1 ca1(size);
ca1.DoTests;
system("pause");
return 0;
}
int CA1::binarySearch(vector<int> v, int target)
{
int top, bottom, middle;
top = vecSize - 1;
bottom = 0;
while (bottom <= top)
{
middle = (top + bottom)/2;
if (v[middle] == target)
return middle;
else if (v[middle] > target)
top = middle - 1;
else
bottom = middle + 1;
}
return -1;
}
double measure(std::function<void()> function) {
auto start_time = std::chrono::high_resolution_clock::now();
function();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>
(std::chrono::high_resolution_clock::now() - start_time);
//std::cout << test << " " << static_cast<double>(duration.count()) * 0.000001 <<
// " ms" << std::endl;
return static_cast<double>(duration.count()) * 0.000001;
}
void CA1::DoTests() {
double time = measure(CA1::binarySearch(vectorUnordered,2));
cout << time << endl;
}
但我發現了錯誤拋出,
error C3867: 'CA1::DoTests': function call missing argument list; use '&CA1::DoTests' to create a pointer to member
functional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<int,false>::_ApplyX<_Rx,>(void)' being compiled
任何想法,我哪裏出錯了?
編輯2
@Rob肯尼迪
我試圖執行代碼的std ::綁定,但我不能讓我的頭周圍, 我已經改變了我的代碼如下:
double CA1::measure(std::function<void()> function) {
auto startCpu = boost::chrono::process_real_cpu_clock::now();
auto startWall = boost::chrono::process_system_cpu_clock::now();
function();
auto durationCpu = boost::chrono::duration_cast<boost::chrono::nanoseconds>
(boost::chrono::process_real_cpu_clock::now() - startCpu);
auto durationWall = boost::chrono::duration_cast<boost::chrono::nanoseconds>
(boost::chrono::process_system_cpu_clock::now() - startWall);
double cpuTime = static_cast<double>(durationCpu.count()) * 0.000001;
double wallTime = static_cast<double>(durationWall.count()) * 0.000001;
/*return static_cast<double>(duration.count()) * 0.000001;*/
cout << "Cpu time " << cpuTime << endl;
cout << "Wall time " << wallTime << endl;
return cpuTime;
}
void CA1::DoTests() {
auto time = measure(std::bind(binarySearch, vectorUnordered, 2));
}
錯誤拋出:
error C3867: 'CA1::binarySearch': function call missing argument list; use '&CA1::binarySearch' to create a pointer to member
我把std :: bind放在正確的地方嗎?我是否需要更改measure()中的參數? 它到底在做什麼?
錯誤消息你所看到的與計時器完全無關。你沒有向你的'measure'函數傳遞*函數*;你傳遞一個'int' - 當你調用*'binarySearch'時返回的那個。 – 2014-10-22 22:59:42
@johntk,'ca1.DoTests;'應該是'ca1.DoTests'(); – Surt 2014-10-22 23:09:37
@Rob Kennedy我現在看到,有沒有什麼辦法可以傳遞這樣的函數,它具有返回值? – Johntk 2014-10-23 09:40:04