save data to csv
// elapse.h
#ifndef __ELAPSE_MILLSEC_H__ #define __ELAPSE_MILLSEC_H__ //#include <iostream> #include <chrono> #include <iomanip> // 用于设置输出流的格式 using namespace std; //计算耗时 class ElapseMillsec{ public: ElapseMillsec(std::string comment); ElapseMillsec(); ~ElapseMillsec(); private: std::string m_comment=""; std::chrono::high_resolution_clock::time_point m_Start; std::chrono::high_resolution_clock::time_point m_End; }; #endif
// elapse.cpp
#include "elapse.h" ElapseMillsec::ElapseMillsec(std::string comment):m_comment(comment){ m_Start = std::chrono::high_resolution_clock::now(); // 获取开始时间 } ElapseMillsec::ElapseMillsec(){ m_Start = std::chrono::high_resolution_clock::now(); // 获取开始时间 } ElapseMillsec::~ElapseMillsec(){ m_End = std::chrono::high_resolution_clock::now(); // 获取结束时间 // 计算持续时间 std::chrono::duration<double, std::milli> elapsed = m_End - m_Start; // 输出执行时间,以毫秒为单位 printf("%s cost %f milliseconds.\r\n", m_comment.c_str(), elapsed.count()); }
// save_csv.cpp
#include "elapse.h" #include <iostream> #include <vector> #include <fstream> typedef struct{ int iID = 0; float fMaxTemp = 0.0f; float fMinTemp = 0.0f; float fAvgTemp = 0.0f; std::string strName=""; }tempDef; void test(){ const int iMaxSize = 1000000; std::vector<tempDef> vecList; // add data to vector { ElapseMillsec elap_vecpush("elap_vecpush"); // 211ms vecList.reserve(iMaxSize*1.2); for(int i=0;i<iMaxSize;i++){ tempDef td; td.iID = i; td.fMaxTemp = i+0.1f; td.fMinTemp = i+0.1f; td.fAvgTemp = i+0.1f; td.strName = std::string("test_") + std::to_string(i); vecList.push_back(td); } } // save data to csv { ElapseMillsec elap_save("elap_save"); // 如果文件不存在则创建;如果存在则覆盖 std::ofstream ofs("test.csv", std::ios::out); // 2546ms --> 100w data -->也和设备性能有关系 ofs << "id,name,max,min,avg\n"; for(auto& td : vecList){ ofs << td.iID << "," << td.strName << "," << td.fMaxTemp << "," << td.fMinTemp << "," << td.fAvgTemp << "\n"; } ofs.close(); std::cout << "save done" << std::endl; } } int main(){ test(); return 0; }
目前,在我电脑的虚拟机上面的运行情况如下:
100w条数据
数据 加入到vector,耗时:211ms
数据写入到 cvs 文件,耗时:2546ms
100w条数据,文件大小:40044470 byte(38.1Mb)