Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fastest way to convert array to multiline string

what is the most efficient way to export array elements into a .csv excel file? Now I am doing like this, but it is very slow. Thank you for all your support.

int FrameWidth = 640;
int FrameHeight = 480;
Int16[] Values;  // 640 x 480, 307200 elements

/*
.. processing ...... 
*/

//Exporting Values to .csv file
string string2csv = null;
for (int y = 0; y < FrameHeight; y++)
{
    for (int x = 0; x < FrameWidth; x++)
    {
        string2csv = string2csv + Values[y * FrameWidth + x] + ";";
    }
    string2csv = string2csv + "\n";
}
File.WriteAllText("string2csv.csv", string2csv);
like image 759
atteee Avatar asked Dec 02 '25 20:12

atteee


2 Answers

One alternative is to combine File.WriteAllLines and string.Join;

File.WriteAllLines ("test.txt", 
    Values.Select((x,i) => new {x, i})
          .GroupBy(x => x.i / FrameWidth)
          .Select(grp => string.Join(";", grp.Select(y => y.x)))
);

It will select all elements along with the index in the array.
It will then group the values by row (calculated by dividing the index by the number of columns).
The grouped values are then joined using ; into a string per row, and the rows are all fed into File.WriteAllLines.

like image 148
Joachim Isaksson Avatar answered Dec 04 '25 08:12

Joachim Isaksson


"most efficient" wil take trial and error, but one easy alternative is to use StringBuilder:

StringBuilder string2csv = new StringBuilder();
for (int y = 0; y < FrameHeight; y++)
{
    for (int x = 0; x < FrameWidth; x++)
    {
        string2csv.Append(Values[y * FrameWidth + x] + ";");
    }
    string2csv.Append("\n");
}

Adding strings using + allocates memory for a new string, then copies the contents of the two strings into the new memory. StringBuilder pre-allocates a buffer in memory and adds characters to the buffer, extending it as necessary. There's much less memory allocation and copying when adding lots of strings.

That may be "fast enough". Other than that, you'd need to get a decent profiler to see where your code is spending the most time and attack that part. It's entirely possible that your "processing..." block is the bottleneck, not the CSV output.

like image 30
D Stanley Avatar answered Dec 04 '25 10:12

D Stanley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!