I have write this code that read a txt file and re-write it whith the timestamp sorted.

This is the result (top image is input, bottom is output) Screenshot

MY QUESTION IS: Anyone do you have a suggestion to optimize this code? Because this text is a little example,my code would work with txt more longer than it. (For example 50K-60K line)


static void Main(string[] args)

    string[] str;            

    //Define regex string for MEX ETCS
    string messageETCS = @"(\d){4}-(\d){2}-(\d){2}.*(?:\r?\n(?!\d{4}-\d{2}-\d).*)*";
    Regex regMEXETCS = new Regex(messageETCS);

    string regexDate = @"\d{4}-\d{2}-\d{2}";
    Regex regDateETCS = new Regex(regexDate);

    // Open the text file using a stream reader.
    using (StreamReader sr = new StreamReader(@"C:\input.txt"))
        // Read the stream to a string, and write the string to the console.
        String logContent = sr.ReadToEnd();

        //run regex
        List<String> MasterList = new List<String>(logContent.Length);
        Match matchResults = regMEXETCS.Match(logContent);
        Match onlyDate = regDateETCS.Match(logContent);
        str = Regex.Split(logContent, @"\d{4}-\d{2}-\d{2}", RegexOptions.None);

        while (matchResults.Success)

            for (int j = 1; j < str.Length;j++ ) {

            string t = str[j].TrimEnd();
            matchResults = matchResults.NextMatch();

        String date1 = onlyDate.ToString();
        using (TextWriter tw = new StreamWriter(@"C:\output.txt"))
            for (int i = 0; i < MasterList.Count; i++)
                tw.WriteLine(date1 + MasterList[i]);

closed as off-topic by t3chb0t, Billal Begueradj, Daniel, Stephen Rauch, Mast Jun 21 '18 at 17:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Billal Begueradj, Daniel, Stephen Rauch, Mast
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 8
    \$\begingroup\$ Can you please replace the images with plain text? To format it well, you can use code-block format: copy-paste the text and press control K. \$\endgroup\$ – janos May 7 '17 at 15:17
  • 3
    \$\begingroup\$ It is difficult to tell just what can be done, because it isn't clear what format the input has. You are splitting by stuff that looks like a date, but with a better specification we can do a better job improving the input. As TheQult says in his answer, reading line-by-line will help a lot (List has good addition characteristics, so don't worry about defining the capacity in advance: what you have currently is way too big and runs the risk of exceeding the 2GB limit on strings). \$\endgroup\$ – VisualMelon May 27 '17 at 8:47

I'm quite surprised that the messageETCS regex actually works. Probably the best and fast way it's to use a parser (ex: https://github.com/picoe/Eto.Parse).

To improve performances, read and process the file chunk by chunck, or line by line if those are lines


Not the answer you're looking for? Browse other questions tagged or ask your own question.