存储提升::计时器产生一个变量

Store boost::timer results in a variable

本文关键字:一个 变量 计时器 存储      更新时间:2023-10-16

我正在写一些排序和搜索算法,并为大学作业测试它们,我必须得到处理不同测试所用的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 vectorn";
    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()中的参数吗?它到底在做什么?

您使用boost::timer::auto_cpu_timer,但应在此处使用boost::timer::cpu_timer血腥细节

为了方便测量,只需将您最喜欢的计时器功能与std::chrono details:交换即可

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