C+中的定时逻辑错误+;(使用std::chrono) 我编写了一个C++程序来对各种排序算法进行基准测试,以找出哪一个是最快的。但是,我在执行代码时遇到了一些问题
我首先创建了一个类,使用构造函数和析构函数对算法计时。然后我使用C+中的定时逻辑错误+;(使用std::chrono) 我编写了一个C++程序来对各种排序算法进行基准测试,以找出哪一个是最快的。但是,我在执行代码时遇到了一些问题,c++,visual-studio,visual-c++,c++17,C++,Visual Studio,Visual C++,C++17,我首先创建了一个类,使用构造函数和析构函数对算法计时。然后我使用std::chrono::time\u point\u cast将时间显式转换为毫秒。然而,每次我运行我的程序时,程序都显示已经过了零毫秒 请注意,我已经包括了chrono头文件 下面是相关程序源代码的一部分 类定义 int Array[20], size = 20; class BenchmarkTimer { public: std::chrono::time_point<std::chrono::high_re
std::chrono::time\u point\u 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;
}
};
但我希望输出是:
Time taken to sort the array: 13 ms
(13毫秒只是一个例子。)
我建议你提出更简单的解决方案,因为我处于C++程序的中级水平。
提前谢谢 这里的问题是,您只能在
基准计时器的析构函数中进行时间计算。这意味着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;
}
这里的问题是,您只能在基准计时器的析构函数中进行时间计算。这意味着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;
}
解决这个问题的一个非常简单的方法是将计时器的print语句放在~BenchmarkTimer()
中。BenchmarkTimer
构造函数可以在~BenchmarkTimer()。这可能看起来像:
class BenchmarkTimer
{
public:
std::string msg_;
std::chrono::steady_clock::time_point startpt_;
explicit BenchmarkTimer(std::string msg)
: msg_(std::move(msg))
, startpt_(std::chrono::steady_clock::now())
{
}
~BenchmarkTimer()
{
using namespace std::chrono;
auto endpt = steady_clock::now();
std::cout << msg_ << duration_cast<milliseconds>(endpt - startpt_).count() << " ms\n";
}
BenchmarkTimer(BenchmarkTimer const&) = delete;
BenchmarkTimer& operator=(BenchmarkTimer const&) = delete;
};
解决这个问题的一个非常简单的方法是将计时器的print语句放在~BenchmarkTimer()
中。BenchmarkTimer
构造函数可以在~BenchmarkTimer()。这可能看起来像:
class BenchmarkTimer
{
public:
std::string msg_;
std::chrono::steady_clock::time_point startpt_;
explicit BenchmarkTimer(std::string msg)
: msg_(std::move(msg))
, startpt_(std::chrono::steady_clock::now())
{
}
~BenchmarkTimer()
{
using namespace std::chrono;
auto endpt = steady_clock::now();
std::cout << msg_ << duration_cast<milliseconds>(endpt - startpt_).count() << " ms\n";
}
BenchmarkTimer(BenchmarkTimer const&) = delete;
BenchmarkTimer& operator=(BenchmarkTimer const&) = delete;
};
是的。我甚至尝试使用纳秒
。您计算析构函数中的持续时间,并且在调用计时器时析构函数尚未被调用。持续时间
。将析构函数代码移动到一个名为get_appeased_time()
的函数中,该函数返回属性duration
是的,我这样做了。我甚至尝试使用纳秒
。您计算析构函数中的持续时间,并且在调用计时器时析构函数尚未被调用。持续时间
。将析构函数代码移动到一个名为get_appeased_time()
的函数中,该函数返回属性duration
我希望我可以合并这个建议,但我发现它有点高级。我觉得rafix07的建议更简单,但也更可行。不过,谢谢你的建议。@MithunK没问题。希望有一天您看到这段代码时不会觉得它太高级了。如果你很好奇,我们有一大堆讨论现代C++和模板的书。它们在某个时候值得一读。谢谢你分享这个链接。我希望我能采纳这个建议,但我发现它有点先进。我觉得rafix07的建议更简单,但也更可行。不过,谢谢你的建议。@MithunK没问题。希望有一天您看到这段代码时不会觉得它太高级了。如果你很好奇,我们有一大堆讨论现代C++和模板的书。它们在某些时候值得一读。感谢您共享此链接。explicit
关键字有什么用?它可以防止std::string
隐式转换为BenchmarkTimer
。相反,必须使用显式语法,如在SelectionSort
中使用的示例所示。我可能还应该修改我的答案,给出BenchmarkTimer
已删除的复制成员,因为这是不寻常的(可能是一个bug)延长BenchmarkTimer
对象的生存期。explicit
关键字的用途是什么?它防止std::string
隐式转换为BenchmarkTimer
。相反,必须使用显式语法,如在SelectionSort
中使用的示例所示。我可能还应该修改我的答案,给出BenchmarkTimer
已删除的复制成员,因为延长BenchmarkTimer
对象的生命周期是不寻常的(很可能是一个bug)。
class BenchmarkTimer
{
public:
std::string msg_;
std::chrono::steady_clock::time_point startpt_;
explicit BenchmarkTimer(std::string msg)
: msg_(std::move(msg))
, startpt_(std::chrono::steady_clock::now())
{
}
~BenchmarkTimer()
{
using namespace std::chrono;
auto endpt = steady_clock::now();
std::cout << msg_ << duration_cast<milliseconds>(endpt - startpt_).count() << " ms\n";
}
BenchmarkTimer(BenchmarkTimer const&) = delete;
BenchmarkTimer& operator=(BenchmarkTimer const&) = delete;
};
void SelectionSort(int Array[])
{
BenchmarkTimer timer{"\nTime taken to sort the array: "};
int temp, smallest, position, j;
// ...
DisplayArray(Array);
} // Output: Time taken to sort the array: 13 ms