2014-10-22 53 views
0

我正在写一些某种形式和搜索算法和测试他们为大学分配,我得的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()中的参数? 它到底在做什么?

+1

错误消息你所看到的与计时器完全无关。你没有向你的'measure'函数传递*函数*;你传递一个'int' - 当你调用*'binarySearch'时返回的那个。 – 2014-10-22 22:59:42

+1

@johntk,'ca1.DoTests;'应该是'ca1.DoTests'(); – Surt 2014-10-22 23:09:37

+0

@Rob Kennedy我现在看到,有没有什么办法可以传递这样的函数,它具有返回值? – Johntk 2014-10-23 09:40:04

回答

1

你使用boost::timer::auto_cpu_timer而是应该选用boost::timer::cpu_timer血淋淋的细节here

为了方便测量使用这样的事情,只是在std交换自己喜欢的定时器功能::计时细节:

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); 

    return static_cast<double>(duration.count()) * 0.000001; 
} 

void Test1() { 
    ... setup test 
    ... call test 
    ... validate return 
} 

void DoTests() { 
    double time = measure(Test1); 
    ... 
    ... profit! 
} 
+0

好,所以我想我理解你的代码纠正了我对我的错误, 所以,你创建了一个名为measure的方法,它返回一个double并接受一个字符串和一个函数作为参数, 然后你启动计时器, 调用传入函数, 获取运行时间,将其转换为double并返回结果。 假设这是正确的,纠正我,如果它错了,为什么你传递字符串?我们已经尝试了您的代码 void CA1 :: DoTests(){ \t double time = measure(“Test1”,CA1 :: binarySearch(vectorUnordered,2)); \t \t cout << time << endl; } – Johntk 2014-10-22 22:27:01

+1

@johntk,measure会返回MS中使用的时间,但是您的系统计时器可能并不准确,请参阅顶部的链接。这个想法是,你不会污染你想用仪器测量的功能,并可以重新使用定时代码。 – Surt 2014-10-22 22:33:36

+0

这里不需要字符串,您可以删除该代码。 – Surt 2014-10-22 22:34:35