c# - Writing many rows into a file efficently -


i have process gets many rows of table. rows need reported in report sheet. i'm doing moment slow, because there many rows (600.000 <= x)-rows. i'll show i'm doing give idea of i'm doing:

 private void shownotfoundrows() {          report += "not found - rows";         report += "\r\n";         report += "in current:";         report += "\r\n";         string columns = "|";         foreach (string header in currentmodel.columns) {             columns += header + "|";         }         report += columns;         report += "\r\n";         foreach (string row in newmodel.keys) {             report += row + "\r\n";         }         report += "\r\n";         report += "in new:";         report += "\r\n";         report += columns;         report += "\r\n";         foreach (string row in currentmodel.keys) {             report += row + "\r\n";         } } 

and after:

        private void createreportfile() {         mylog.writetolog("creating reportfile "+newmodel.tablename, mylog.messages.info);         string dir = settings.default.reportfolder + "\\" + directoryname + " " + datetime.now.tostring("dd-mm-yyyy");         directory.createdirectory(dir);          try {             file.writealltext(dir + "\\report " + newmodel.tablename, report);         } catch (directorynotfoundexception e) {              console.writeline(e.message + "\n" + e.stacktrace);         }     } 

start using stringbuilder , see gets you. you're copying in lot of memory stringx = stringx+ stringy because creates new object , copies of new object. have feeling that'll need be. stringbuilder contrast preallocates memory.

beyond that, things divide data partitions, , multithread that, , combine results @ end.


Comments