存储boost :: timer会产生一个变量

时间:2014-10-22 11:40:26

标签: c++ boost

我正在编写一些排序和搜索算法并测试它们用于大学作业,我必须获得处理不同测试时使用的CPU时间和待机时间。以及个人时间。

我正在使用boost API来实现这一点,我的问题是我必须运行多个测试并获得平均时间,但我找不到一个解决方案来存储结果boost在变量中给出的。

以下是我的算法之一:

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 Kennedy

我试图实现你的代码std :: bind但是我无法理解它, 我改变了我的代码如下:

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 个答案:

答案 0 :(得分:1)

您使用boost::timer::auto_cpu_timer,但应使用boost::timer::cpu_timer血腥详情here

为了方便测量使用这样的东西,只需用std :: chrono细节交换你最喜欢的计时器功能:

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!
}