Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# very slow StreamReader

I'm using some not optimal code written by me... :-|

I have following code:

string fmtLine = "";
            string[] splitedFmtLine;
            int counterFMTlines = 0;

            foreach (string fmtF in fmtFiles)
            {
                using (StreamReader sr = new StreamReader(fmtF))
                {
                    while ((fmtLine = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(counterFMTlines++);
                        foreach (L3Message message in rez)
                        {
                            splitedFmtLine = Regex.Split(fmtLine, "\t");

                            if (message.Time == splitedFmtLine[0])
                            {
                                message.ScramblingCode = splitedFmtLine[7];      
                            }
                        }
                    }
                }
            }

I tested this code when List was empty and there was only one file (tab delimited, 280000 lines), and even then it took lifetime (1 min) to go through all 280000 lines of my file. That means that execution skipped foreach loop where is my List of myObjs.

I cannot understand why it took so long?

As example, I was filling my List of myObjs (tree hierarchy) with different text file (source file) but bigger than this tab delimited(tab delimited: 16MB, source file: 36MB) and it took only second versus this 1 minute.

like image 438
JohnDoeKazama Avatar asked Jun 28 '26 10:06

JohnDoeKazama


1 Answers

You are writing 280.000 times to the console which is very slow. Remove the console output. Also, use string.Split('\t') which is way faster than this particular regex call.

like image 127
usr Avatar answered Jun 30 '26 01:06

usr