1

下面是我的程序,它应该创建一个对象数组,然后告诉。每个循环需要多长时间运行。循环只是从文件和内存中读取/写入,程序应该在完成时比较这些方法。出于某种原因,如果创建的记录数为 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;

}

4

1 回答 1

2

编辑:

这条线

for(int count4 = 0; count4 > randomNumber; count4++)

很危险。您不会仅仅因为迟早会count4环绕而进入无限循环,但这是一个糟糕的情况。2^32-1不应该count 4 < randomNumber吗?

编辑2:

tempInt/tempString 的分配只能进行一次(在循环之前),而不是在循环本身。

老东西:

第一个(相当明显的)建议:停止动态分配所有内容。

代替

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;
}

只做

for(int count2 = 0; count2 < recordsToBeCreated; count2++) //read/write from mem
{
   int tempNumber;
   string tempString;

   tempNumber = recordObj[count2].getNumber();
   tempString = recordObj[count2].getData();
}

正如您应该知道的,tempNumber并且tempString将在堆栈上分配,并且它们在每次迭代结束时被释放。

现在的错误。

在这个循环中

for (int count1 = 0; count1 < recordsToBeCreated; count1++)
{
    outFile.open("data.txt");
    outFile << recordObj[count1] << endl;
}

您在每次迭代时覆盖文件。我想这应该是

outFile.open("data.txt");
// write 'em all
for (int count1 = 0; count1 < recordsToBeCreated; count1++)
{
    outFile << recordObj[count1] << endl;
}

在这个循环中

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;
}

您再次重新打开“data.txt”,因此您只阅读了第一行。

这应该是(删除分配)

inFile.open("data.txt");
for(int count3 = 0; count3 < recordsToBeCreated; count3++) //read/write from disk
{
   int tempNumber;
   string tempString;

   inFile >> tempNumber;
   inFile >> tempString;
}
于 2012-09-18T22:12:12.990 回答