Search code examples
c++performancecsvexport-to-csvcfile

How to make C++ code faster: Creating .csv file from camera data using CFile


A high level overview is that 'CFile file's 'file.write()' method gets called for every individual integer data value (line 9) along with line 12, where I write a comma to file.

That means that for 327,680 input data integers, there are 2*327,680 = 655,360 file.write() calls. The code is very slow because of this and as a result, the code takes 3 seconds to create one csv file. How could I improve the efficiency of my code?

Note: I cannot change any declarations of the code. I must use CFile. Also, pSrc is of type uint_16_t and is containing the data that I want to store in the .csv file. The data ranges from 0 - 3000 integer values.

1           CFile file;
2           int mWidth = 512;
3           int mHeight = 640;
4           UINT i = 0;
5           char buf[80];
6           UINT sz = mHeight * mWidth; //sz = 327,680
7           while (i < sz) {
8                  sprintf_s(buf, sizeof(buf), "%d", pSrc[i]); 
9                  file.Write(buf, strlen(buf));
10                 i++;
11                 if (i < sz)  
12                        file.Write(",", 1);
13                 if (i % mWidth == 0) 
14                        file.Write("\r\n", 2);
15  }

All values are outputted in the 640x512 .csv file containing integers representing degrees Celcius.


Solution

  • Just Figured it out! Below is the implementation that seemed to get the job done.

    int sz = mHeight * mWidth;
    
    std::string uniqueCSV = "Frame " + to_string(miCurrentCSVImage + 1) + ".csv";
    std::string file = capFile + "/" + uniqueCSV;
    std::ofstream out;
    out.open(file);
    
    std::string data = "";
    
    int i = 0;
    while (i < sz) {
        data += to_string(pSrc[i]);
        i++;
        if (i < sz)
            data += ",";
        if (i % mWidth == 0)
            data += "\n";
    }
    
    out << data;
    out.close();
    miCurrentCSVImage++;