C ++中的时序逻辑错误(使用std :: chrono)

问题描述 投票:0回答:1

我编写了一个C ++程序来对各种排序算法进行基准测试,以找出哪种算法最快。但是,我在执行代码时遇到了一些问题。

我首先创建了一个使用构造函数和析构函数对算法计时的类。然后,我使用std::chrono::time_point_cast将时间显式转换为毫秒。但是,该程序最终显示每次我运行程序都经过了零毫秒。

请注意,我已经包含了chrono头文件。

这是涉及的程序源代码的一部分。

类定义

int Array[20], size = 20;

class BenchmarkTimer
{
public:
    std::chrono::time_point<std::chrono::high_resolution_clock> startpt;
    float ms;
    long long duration;
    BenchmarkTimer() : ms(0), duration(0)
    {
        startpt = std::chrono::high_resolution_clock::now();
    }
    ~BenchmarkTimer()
    {
        auto endpt = std::chrono::high_resolution_clock::now();
        auto init = std::chrono::time_point_cast<std::chrono::milliseconds>(startpt).time_since_epoch().count();
        auto final = std::chrono::time_point_cast<std::chrono::milliseconds>(endpt).time_since_epoch().count();
        auto duration = final - init;
    }
};

选择排序功能(这只是许多排序算法之一)。

void SelectionSort(int Array[])
{
    BenchmarkTimer timer;
    int temp, smallest, position, j;
    for (int i = 0; i < size - 1; i++)
    {
        smallest = Array[i];
        position = i;
        for (j = i + 1; j < size; j++)
            if (Array[j] < smallest)
            {
                smallest = Array[j];
                position = j;
            }
        temp = Array[i];
        Array[i] = Array[position];
        Array[position] = temp;
    }
    DisplayArray(Array);
    std::cout << "\nTime taken to sort the array: " << timer.duration << " ms" << std::endl;
}

DisplayArray(Array)函数调用只是将数组显示在屏幕上。

我希望程序显示经过的毫秒数。

现在,实际输出是:

Time taken to sort the array: 0 ms

但是我希望输出为:

Time taken to sort the array: 13 ms

((13毫秒只是一个示例。)

[我建议您建议更简单的解决方案,因为我处于C ++编程的中级水平。

提前感谢!

c++ visual-studio visual-c++ c++17
1个回答
0
投票

第一个问题是您在析构函数中创建新的duration作为局部变量:

auto duration = final - init;

因此,时间测量值未作为成员变量分配给duration。应该是:

duration = final - init;

第二个问题,您需要在计算此值之前访问timer.duarion,因为在执行BenchmarkTimer的主体时将调用SelectionSort的析构函数-必须到达最后一个括号:

    DisplayArray(Array);
    ...  sort the array: " << timer.duration << ... // duration is not calculated 
} // <- here is called dtor of Timer

您应该将dtor的主体移至某个成员函数:

void Timer::stopTimingAndGetResult()
{
    auto endpt = std::chrono::high_resolution_clock::now();
    auto init = std::chrono::time_point_cast<std::chrono::milliseconds>(startpt).time_since_epoch().count();
    auto final = std::chrono::time_point_cast<std::chrono::milliseconds>(endpt).time_since_epoch().count();
    duration = final - init;
}

您可以将其用作:

BenchmarkTimer timer;
int temp, smallest, position, j;
for (int i = 0; i < size - 1; i++)
{
    //...
}
DisplayArray(Array);
timer.stopTimingAndGetResult();
std::cout << "\nTime taken to sort the array: " << timer.duration << " ms" << std::endl;

0
投票

这里的问题是,您只需要在BenchmarkTimer的析构函数中进行时间计算。这意味着duration将始终为0,因为它仅在析构函数中发生变化,并且在对象被破坏后无法访问它。

有几种解决方法。首先是将计时码移入功能。其次,您可以修改BenchmarkTimer以在构造函数中获取一个函数对象,该对象将成为要运行的代码,然后在构造函数中进行计算。看起来像

class BenchmarkTimer
{
public:
    std::chrono::time_point<std::chrono::high_resolution_clock> startpt;
    float ms;
    long long duration;
    template<typename Func>
    BenchmarkTimer(Func func) : ms(0), duration(0)
    {
        startpt = std::chrono::high_resolution_clock::now();
        func();
        auto endpt = std::chrono::high_resolution_clock::now();
        auto diff = end-start;
        duration = diff.count();
    }
};

void SelectionSort(int Array[])
{
    BenchmarkTimer timer([&]()
    {
        int temp, smallest, position, j;
        for (int i = 0; i < size - 1; i++)
        {
            smallest = Array[i];
            position = i;
            for (j = i + 1; j < size; j++)
                if (Array[j] < smallest)
                {
                    smallest = Array[j];
                    position = j;
                }
            temp = Array[i];
            Array[i] = Array[position];
            Array[position] = temp;
        }
    });
    DisplayArray(Array);
    std::cout << "\nTime taken to sort the array: " << timer.duration << " ms" << std::endl;
}

[另一种选择是,在您希望BenchmarkTimer进行计算后,将其添加到另一个函数,然后将析构函数代码移至其中。请注意,在析构函数中声明一个duration变量以隐藏类的duration。代码应类似于

auto endpt = std::chrono::high_resolution_clock::now();
auto init = std::chrono::time_point_cast<std::chrono::milliseconds>(startpt).time_since_epoch().count();
auto final = std::chrono::time_point_cast<std::chrono::milliseconds>(endpt).time_since_epoch().count();
duration = final - init;
// ^ no auto here
© www.soinside.com 2019 - 2024. All rights reserved.