Ostream双重精度

ostream double precision

本文关键字:精度 Ostream      更新时间:2023-10-16

因此,我已经实现了自己的array数据结构。我已经实施了以下操作:

  1. 在指定索引上添加元素
  2. 删除元素-------- || ---------
  3. 检查该值是否存在于数组中

现在,我必须测量这些操作的时间。我有此代码:( 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::fixedstd::cout.precision(10) cout。它按照我的意愿运行。我该如何使其用于保存到文件?

P.S我不幸无法使用boost::

您与cout一起使用的相同操作器可以与fstreams一起使用,而不会出现任何问题。尝试在打印到标准输出时使用相同的代码。