C++ 为什么赢了';我的执行时间不能写在文件中吗?
我正在使用Chrono库来计算我的特定代码行的执行时间。我能够计算执行时间,但当我试图将其写入文件时,会出现各种错误。 我得到的错误是:C++ 为什么赢了';我的执行时间不能写在文件中吗?,c++,c++11,C++,C++11,我正在使用Chrono库来计算我的特定代码行的执行时间。我能够计算执行时间,但当我试图将其写入文件时,会出现各种错误。 我得到的错误是: 操作员不匹配持续时间的问题可以通过duration\u cast解决,另一个问题是行int*arr=new int(sizeof(int)*n)
操作员不匹配持续时间的问题可以通过
duration\u cast
解决,另一个问题是行int*arr=new int(sizeof(int)*n)正如molbdnilo所注意到的,它只分配一个int
,其值为sizeof(int)*n
。您可能还想使用更好的随机数生成器。代码中的建议:
#include <iostream>
#include <fstream>
#include <chrono>
#include <vector> // std::vector
#include <random> // std::random
int main ()
{
std::random_device rd;
std::mt19937 generator(rd()); // seed a prng
// and create a distribution object:
std::uniform_int_distribution<int> rnd_dist(1, 1000); // [1,1000]
// you can open the file directly like this:
std::ofstream plot("graph.txt");
// and check if it was opened successfully:
if(!plot) {
std::clog << "couldn't open file\n";
return 1;
}
// I'll use a std::random generator instead of this:
// srand((unsigned)time(0));
int n = 250;
std::cout <<"The size of the array is:" << n << "\n\n";
plot << n; // you probably want << "\n"; here
// You had:
// int *arr = new int (sizeof(int)*n);
// It should have been:
// int *arr = new int[n];
// but the below is better since you don't have to delete[] it manually:
std::vector<int> arr(n);
for (int i=0; i<n; ++i) {
arr[i] = rnd_dist(generator);
}
std::cout << "The array provided is:\n";
for (int i=0; i<n; ++i) {
std::cout << arr[i] << " ";
}
std::cout << "\n\n";
// Edit: changed to high_resolution_clock
auto start = std::chrono::high_resolution_clock::now();
// the below call works, but consider changing the signaure of your Selectionsort() to:
// void Selectionsort(std::vector<int>& arr);
// The "n" is available via "arr.size();"
Selectionsort(arr.data(), arr.size());
auto end = std::chrono::high_resolution_clock::now();
// diffe duration_cast fix:
auto diffe = std::chrono::duration_cast<std::chrono::milliseconds>(end-start);
plot << diffe.count(); // you probably want << "\n"; here
std::cout << "The execution time for average case is: " << diffe.count() << " ms\n";
}
#包括
#包括
#包括
#include//std::vector
#include//std::random
int main()
{
std::随机_装置rd;
std::mt19937生成器(rd());//为prng种子
//并创建分发对象:
标准:统一国际分布区域(11000);/[11000]
//您可以像这样直接打开文件:
std::流图(“graph.txt”);
//并检查是否已成功打开:
如果(!绘图){
std::clog从输出语句中删除类型声明:std::难道您没有diff
的声明吗?您是想使用diff
?newint(sizeof(int)*n)
分配一个int
值sizeof(int)*n
。建议清理此问题并生成一个。如果制作MCVE不会向您暴露您的问题,请编辑此问题以包含MCVE。是的,我打算使用Differ。很抱歉输入错误,但即使我使用Differ,我仍然收到错误您好,谢谢您的代码。我终于可以执行我的代码并将执行时间写入一个文件中,但问题是它显示的执行时间是0毫秒。@AyushPoudel可能是您的Selectionsort
太快,稳定的时钟无法注意到。我在答案中更改为high\u resolution\u clock
。也许这会注意到一个差异。您还可以尝试将duration\u cast
转换为std::chrono::microseconds
以查看这是否会产生影响。我将其更改为高分辨率时钟,并大幅增加了阵列的大小,但它仍然给我0毫秒。如果我这样做:auto Differ=end start;我确实以毫秒为单位获取时间,但当我尝试使用plot将其写入文件时,您是否检查过阵列是否真的进行了排序?是的,th数组被排序了,我在之前的评论中做了一些编辑。你能检查一下吗
#include <iostream>
#include <fstream>
#include <chrono>
#include <vector> // std::vector
#include <random> // std::random
int main ()
{
std::random_device rd;
std::mt19937 generator(rd()); // seed a prng
// and create a distribution object:
std::uniform_int_distribution<int> rnd_dist(1, 1000); // [1,1000]
// you can open the file directly like this:
std::ofstream plot("graph.txt");
// and check if it was opened successfully:
if(!plot) {
std::clog << "couldn't open file\n";
return 1;
}
// I'll use a std::random generator instead of this:
// srand((unsigned)time(0));
int n = 250;
std::cout <<"The size of the array is:" << n << "\n\n";
plot << n; // you probably want << "\n"; here
// You had:
// int *arr = new int (sizeof(int)*n);
// It should have been:
// int *arr = new int[n];
// but the below is better since you don't have to delete[] it manually:
std::vector<int> arr(n);
for (int i=0; i<n; ++i) {
arr[i] = rnd_dist(generator);
}
std::cout << "The array provided is:\n";
for (int i=0; i<n; ++i) {
std::cout << arr[i] << " ";
}
std::cout << "\n\n";
// Edit: changed to high_resolution_clock
auto start = std::chrono::high_resolution_clock::now();
// the below call works, but consider changing the signaure of your Selectionsort() to:
// void Selectionsort(std::vector<int>& arr);
// The "n" is available via "arr.size();"
Selectionsort(arr.data(), arr.size());
auto end = std::chrono::high_resolution_clock::now();
// diffe duration_cast fix:
auto diffe = std::chrono::duration_cast<std::chrono::milliseconds>(end-start);
plot << diffe.count(); // you probably want << "\n"; here
std::cout << "The execution time for average case is: " << diffe.count() << " ms\n";
}