Ostream双重精度
ostream double precision
因此,我已经实现了自己的array
数据结构。我已经实施了以下操作:
- 在指定索引上添加元素
- 删除元素-------- || ---------
- 检查该值是否存在于数组中
现在,我必须测量这些操作的时间。我有此代码:( 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)
cout
。它按照我的意愿运行。我该如何使其用于保存到文件?
P.S我不幸无法使用boost::
您与cout
一起使用的相同操作器可以与fstreams
一起使用,而不会出现任何问题。尝试在打印到标准输出时使用相同的代码。
相关文章:
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 正在将csv文件读取为双精度矢量
- ostream过载时的缓冲区冲洗
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 如何在C++中的同一函数中使用字符串和双精度
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 需要从 istream 和 ostream 派生 iostream
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 如何计算具有指定类型的表达式的相对精度和绝对精度
- 如何打印boost多精度128位无符号整数
- "ostream &os"有什么用?
- 检查是否以特定精度给出双精度
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- C 字符串返回字符串的整数/双精度/长整型值
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 为什么 Clang std::ostream 写一个 std::istream 无法读取的双精度?
- Ostream双重精度