std::fstream 需要很长时间才能将大数据写入.csv文件中

std::fstream taking very long time to write large data into .csv file

本文关键字:数据 csv 文件 fstream 长时间 std      更新时间:2023-10-16

我正在尝试将大数据写入csv文件,我正在处理的行数以百万为单位。

我的代码正在以双精度数组的形式生成行。代码将数据写入文件需要很长时间(比在同一台计算机上使用 10k 批量插入将相同数量的记录写入数据库的两倍(。

我尝试在 std::string 中缓冲 10000 和 100000 行,但它仍然很慢。

HRESULT FastLoad::Insert()
{
for (int i = 1; i <= m_iCsvColumnCount; i++)  // m_iCsvColumnCount is actual number of columns 1 to n
{
if (arrayOfRowVals[i] != NULL_NUM)    // arrayOfRowVals is array of double 
m_Csvfile << arrayOfRowVals[i];       // std::fstream  m_Csvfile.open("/install/FactPOC/Fact.csv", std::ios_base::out | std::ios_base::app);
if (i < m_iCsvColumnCount)
{
m_Csvfile << ",";
}
}
m_Csvfile << "n";
return 0;
}

您一遍又一遍地打开文件。这注定会很慢!!

相反,请打开它一次。为函数提供对可以重用的ifstream的引用。

并删除缓冲。在你和文件系统之间已经有很多现有的、更好的缓冲层。