C++快速写入文本文件
C++ write to text file fast
我有大量值(整数)范围从 -1 到 15 的大矩阵,我想用下面的函数将它们写入文本文件。写入速度似乎约为 0.1 MB/s,所以我玩了一下,看看是否可以让它更快而没有任何结果。如何使其更快?
bool mymap::write_one_mat(string txtfile, matrix& mat)
{
ofstream myfile (txtfile, ios::app|ios::binary);
int element;
if (myfile.is_open())
{
int rows = mat.get_rows();
int cols = mat.get_cols();
myfile << "<";
for(int i = 1; i <= rows; ++i)
{
for(int j = 1; j <= cols; ++j)
{
element = mat.get_element(i,j);
if(element < 0 || element > 9)
{
myfile << to_string(element);
}
else
{
myfile << " ";
myfile << to_string(element);
}
}
}
myfile << ">n";
myfile.close();
return true;
}
else
return false;
}
正如已经评论的那样,您可能希望开始删除不必要的std::to_string()
使用:流可以直接愉快地格式化整数。但是,即使直接格式化整数,也有一些不必要的开销查找有关分面的信息,这似乎在大多数实现中使用了dynamic_cast<...>(..)
。因此,使用如下方法手动格式化整数可能会更快:
std::locale loc(std::locale(), new std::num_put<char, char*>());
std::num_put<char, char*> const& np(std::use_fast<std::num_put<char, char*>>(loc));
char buffer[1024];
char* next(buffer);
for (int i(1); i <= rows; ++i) {
for (int j(1); j <= cols; ++j) {
int element(mat.get_element(i, j));
if (element < 0 || element < 9) {
*next++ = ' ';
}
next = np.put(next, myfile, ' ', element);
if (std::numeric_limits<int>::digits10 + 1 <= (buffer + 1014) - next)) {
myfile.write(buffer, next - buffer);
next = buffer;
}
}
}
myfile.sputn(buffer, next - buffer);
直接使用std::num_put<...>
似乎是最快的方法(请参阅此图,其中绘制了使用不同方法的不同编译器所花费的时间:越短越好)。
似乎您的代码编写了一个大的数字序列,其中包含一些引入空格的奇怪规则:您确定不想在每个element
之后放置一个空格,也许在每行之后放置一个换行符吗?
从您拥有的评论中获取:
bool mymap::write_one_mat(std::string const& txtfile, matrix const& mat)
{
std::ios_base::sync_with_stdio(false);
std::ofstream myfile(txtfile, std::ios_base::app | std::ios_base::binary);
if (myfile.is_open())
{
int rows = mat.get_rows();
int cols = mat.get_cols();
myfile << "<";
for (int i = 1; i <= rows; ++i)
{
for (int j = 1; j <= cols; ++j)
{
int element = mat.get_element(i, j);
if (!(element < 0 || element > 9))
myfile << " ";
myfile << element;
}
}
myfile << ">n";
}
return static_cast<bool>(myfile);
}
此外,txtfile
和 mat
的类型已更改为对 const
的引用。这是有道理的,因为您的write_one_mat
方法不会修改其参数。确保mat::get_rows()
、mat::get_cols()
和get_element()
是const
方法,以便可以在mat
上调用它们。
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 已修改的LinkedList未在文本文件本身中更新
- C++文本文件的获取线
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- 打印逐行存储的文本文件
- C++文本文件输入