为什么我的执行时间不会写入文件?

  • 本文关键字:文件 执行时间 c++ c++11
  • 更新时间 :
  • 英文 :


我正在使用Chrono库来计算我特定的代码行的执行时间。我能够计算执行时间,但是当我尝试在文件中写入时,我会遇到各种错误。我遇到的错误是:

操作员不匹配<

无法将diffe转换为类型const unsigned char*

这是我的代码的实例

    int main ()
{   
    ofstream plot;
    plot.open("graph.txt");
    srand((unsigned)time(0));    

        int n = 250;
        std::cout <<"The size of the array is:" << n << std::endl;
        std::cout <<"n";
        plot << n;
        int *arr = new int (sizeof(int)*n);
        for (int i=0;i<n;i++)
        {
            int number = (rand()%1000+1);
            arr[i] = number;
        }
        std::cout << "The array provided is:" << std::endl;
        for (int i=0;i<n;i++)
        {
            std::cout << arr[i] << " ";
        }
        std::cout << "n";
        std::cout<<"n";
                auto start = chrono::steady_clock::now();
                Selectionsort (arr,n);
                auto end = chrono::steady_clock::now();
                auto diffe = end-start; 
                double  a = (double ) diff;
                plot << diff;
                std::cout << "The execution time for average case is:" <<
                std::cout << chrono::duration <double, milli> (diffe).count() << " ms" << std::endl;

plot&lt;&lt;差异;这是我遇到错误的原因。我在此代码中所做的是计算最佳,最差和平均情况的执行时间,并将数组和数据的大小和数据传输到文件以绘制图形。在

之前,我没有使用Chrono库的经验

持续时间的问题可以用 duration_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 filen";
        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 << "nn";
    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 << "nn";
    // 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() << " msn";
}

最新更新