下面是我的程序,它应该创建一个对象数组,然后告诉。每个循环需要多长时间运行。循环只是从文件和内存中读取/写入,程序应该在完成时比较这些方法。出于某种原因,如果创建的记录数为 16 或更少,程序只会显示时间。我似乎无法找出原因。我也看不出为什么我的比率不起作用。可能是因为每个变量都小于一秒,所以它可能试图将 0 除以 0?
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <iomanip>
using namespace std;
class TestRecord
{
private:
int _number;
string _data;
public:
TestRecord ()
{_number = 0; _data = "no data";}
TestRecord (int number, string data)
{_number = number; _data = data;}
void setNumber (int number)
{_number = number;}
void setData (string data)
{_data = data;}
int getNumber() const
{return _number;}
string getData() const
{return _data;}
friend ostream &operator <<(ostream &out, TestRecord &records)
{
out << records._number << " " << records._data;
return out;
}
};
int main ()
{
string string256 = "AaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronAaronA";
int recordsToBeCreated = 0;
cout << " How many records should be created?" << endl;
cin >> recordsToBeCreated;
TestRecord* recordObj = new TestRecord[recordsToBeCreated];
//--------------------------------------writing
clock_t startTime1 = clock();
for (int count = 0; count < recordsToBeCreated; count++) //fill object array
{
srand(count);
recordObj[count].setData(string256);
recordObj[count].setNumber(rand()% 10000 + 1000);
}
clock_t endTime1 = clock();
ofstream outFile;
clock_t startTime2 = clock();
for (int count1 = 0; count1 < recordsToBeCreated; count1++) //write to disk
{
outFile.open("data.txt");
outFile << recordObj[count1] << endl;
}
clock_t endTime2 = clock();
outFile.close();
//----------------------------------------reading
clock_t startTime3 = clock();
for(int count2 = 0; count2 < recordsToBeCreated; count2++) //read/write from mem
{
int *tempNumber = new int;
string *tempString = new string;
*tempNumber = recordObj[count2].getNumber();
*tempString = recordObj[count2].getData();
delete tempNumber;
delete tempString;
}
clock_t endTime3 = clock();
ifstream inFile;
clock_t startTime4 = clock();
for(int count3 = 0; count3 < recordsToBeCreated; count3++) //read/write from disk
{
int *tempNumber = new int;
string *tempString = new string;
inFile.open("data.txt");
inFile >> *tempNumber;
inFile >> *tempString;
delete tempNumber;
delete tempString;
}
clock_t endTime4 = clock();
inFile.close();
//---------------------------------random reading comp
int randomNumber = rand()%recordsToBeCreated;
clock_t startTime5 = clock(); //traverse array to random index
for(int count4 = 0; count4 > randomNumber; count4++)
{
recordObj[count4];
}
clock_t endTime5 = clock();
clock_t startTime6 = clock();
inFile.open("data.txt");
for(int count5 = 0; count5 < randomNumber; count5++)
{
string *tempString = new string;
cin.ignore();
inFile >> *tempString;
}
clock_t endTime6 = clock();
inFile.close();
double seconds1 = endTime1 - startTime1;
seconds1 = seconds1 / CLOCKS_PER_SEC;
double seconds2 = endTime2 - startTime2;
seconds2 = seconds2 / CLOCKS_PER_SEC;
double seconds3 = endTime3 - startTime3;
seconds3 = seconds3 / CLOCKS_PER_SEC;
double seconds4 = endTime4 - startTime4;
seconds4 = seconds4 / CLOCKS_PER_SEC;
double seconds5 = endTime5 - startTime5;
seconds5 = seconds5 / CLOCKS_PER_SEC;
double seconds6 = endTime6 - startTime6;
seconds6 = seconds6 / CLOCKS_PER_SEC;
cout << "Time to write to memory: " << fixed << setprecision (5) << seconds1 << " seconds" << endl;
cout << "Time to write to file: " << setprecision (5) << seconds2 << " seconds" << endl;
cout << "Ratio of (write to disk)/(write to memory): " << seconds2/seconds1 << endl;
cout << "\n\n";
cout << "Runtime to read from memory: " << setprecision (5) << seconds3 << " seconds" << endl;
cout << "Runtime to read from disk: " << setprecision (5) << seconds4 << " seconds" << endl;
cout << "Ratio of (read from disk)/(read from memory): " << seconds4/seconds3 << endl;
cout << "\n\n";
cout << "Runtime to find random index from memory: " << setprecision (5) << seconds5 << " seconds" << endl;
cout << "Runtime to find random line from disk: " << setprecision (5) << seconds6 << " seconds" << endl;
cout << "Ratio of (read from disk)/(read from memory): " << seconds6/seconds5 << endl;
delete [] recordObj;
system("pause");
return 0;
}