我想输出我的C ++程序运行到桌面上名为C10.txt的文件的时间。我想运行程序100次,然后当我打开文件时,它会告诉我程序运行的所有不同时间。我知道我需要fstream这样做,但我不知道从那里去哪里。这是我想多次运行的代码。我应该添加什么才能使其发挥作用?
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
void merge(int*,int,int);
void mergeSort(int*,int,int);
const int ARRAYSIZE = 10;
int main()
{
clock_t startTime = clock();
srand(unsigned(time(0)));
cout << (double( clock() - startTime ) / (double)CLOCKS_PER_SEC) * 1000000<< " microseconds." << endl;
return 0;
}
答案 0 :(得分:0)
在main()中,在实际执行任何操作之前创建ofstream yournamehere ("path_to_desktop\C10.txt");
。
然后只需在for循环中调用您的代码,在开头将clock()
分配给startTime,然后将您现在写的内容写到cout
,改为yournamehere
。
答案 1 :(得分:0)
在追加模式下打开一个流。
std::ofstream tlog("C10.txt", std::ofstream::app);
。
主要是,将时钟记录写入tlog
而不是std::cout
。
您对std::endl
的使用是浪费的。 Endl刷新流,听起来并不像你需要的那样。只需直接使用换行符“\ n”。