i wish to know if there is a way to copy data to a file faster then the following approach used below ? What i am doing is to write matrix/array data to a .cvs file. there are on an average 150,000 elements in the array/matrix with each element having three double data type members.The code is here:
var path = Path.Combine(myData, "KinectData-" + time + ".txt");
// start writing the data to file
for (int i = 0; i < maxLen; i++)
{
System.IO.File.AppendAllText(@path, Convert.ToString(p1[i].X) + " ," + Convert.ToString(p1[i].Y) + " ," + Convert.ToString(p1[i].Z) + Environment.NewLine);
System.IO.File.AppendAllText(@path, Convert.ToString(p2[i].X) + " ," + Convert.ToString(p2[i].Y) + " ," + Convert.ToString(p2[i].Z) + Environment.NewLine);
}
The time taken for this process is close to 3 min which i wish to minimize.any solution will be of great value to me.
Thank you

VulpesPosted Feb 27, 2015, 8:36 AM
Sanju SinghPosted Feb 28, 2015, 12:36 AM
VulpesPosted Feb 27, 2015, 11:56 AM
1. You were doing a lot of string concatenations.
2. You were appending to the file after every iteration and - worse still - opening and closing it each time.
The first problem can be solved by using a StringBuilder and the second by only appending to the file after (say) every 1000 iterations and by opening and closing the file just the once.
Also, if we make the capacity of the StringBuilder big enough then it won't need to keep increasing the size of its internal buffer which slows things down. So, I figured a maximum of 20 characters for each of the 6 doubles, 8 characters for the spaces/commas and 4 characters for the new lines (\r\n), making 132 in total, multiply by 1000 to give 132,000.
Also, if you just clear the StringBuilder after each batch (by setting its Length to zero), one instance of it will suffice :)
Sanju SinghPosted Feb 27, 2015, 11:08 AM
Sanju SinghPosted Feb 27, 2015, 10:34 AM
NaN,NaN,NaN
0,0,0
NaN,NaN,NaN
0,0,0
NaN,NaN,NaN
0,0,0 N
aN,NaN,NaN
0,0,0 N
aN,NaN,NaN
0,0,0
NaN,NaN,NaN
0,0,0
and so on.