所以我实现了我自己的array
数据结构。我实现的操作,如:ostream double precision
- 在指定索引
- 添加元素删除元素-------- -------- ||
- 察看值存在于数组中
现在我必须测量这些操作的时间。我有这样的代码:(IM使用Visual Studio C++)
LARGE_INTEGER clock, start, end, result;
QueryPerformanceFrequency(&clock);
int sizes[] = { 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000 };
long double seconds;
long double summedSeconds = 0;
std::ofstream myfile;
myfile.open("results.txt");
std::setprecision(10); //I want the precision to be 1ns + 1 bit for rounding
for (auto&x : sizes)
{
for (int i = 0 ; i < 100; i++)
{
myArray.generate(x); // this generates myArray of size x
QueryPerformanceCounter(&start);
myArray.insert(1, x/2); //this will insert value of 1 into an index = half of array
QueryPerformanceCounter(&end);
result.QuadPart = end.QuadPart - start.QuadPart;
seconds = (long double)result.QuadPart/(long double)clock.QuadPart;
summedSeconds += seconds; // this is summed up for 100 example data
}
std::cout << summedSeconds/100 << '\n';
myfile << std::fixed << std::setw(6) << x << "\t" << summedSeconds/100 << '\n';
}
myfile.close();
现在,这给了我在results.txt
是这样的:
100 0.000008
200 0.000013
500 0.000031
1000 0.000052
2000 0.000115
5000 0.000287
10000 0.000568
20000 0.001134
50000 0.002017
100000 0.003756
因此,基于元素的数量,时间测量。但讲师要求~1ns
精度,所以这还不够(现在只有6位,我想至少要9-10)。当我还没有将它保存到文件中时,我使用std::fixed
和std::cout.precision(10)
来记录该信息。它按我的意思工作。我怎样才能使它保存到文件?
P.S我很遗憾不能使用boost::
无论谁告诉你'precision()'不适用于'std :: ofstream',你正在使用,显然是错误的。 –
但后来它是'std :: cout.precision(10)',现在应该用什么替换'cout'? @编辑,好吧,你是对的。 'myfile.precision()'做了这个工作 – Frynio