我正在写一些某种形式和搜索算法和测试他们为大学分配,我得的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