1
\$\begingroup\$

We have a

string text = 
"SOME OTHER TEXT
WHITE SPACES AND NEW LINES

[HitObjects]
109,192,7241,1,0,0:0:0:0:
256,192,7413,1,0,0:0:0:0:
475,192,75865,1,0,0:0:0:0:
329,192,86524,1,0,0:0:0:0:
182,192,256242,1,0,0:0:0:0:
256,192,306521,1,2,0:0:0:0:

WHITE SPACES AND NEW LINES
"

The third number of every row is the milliseconds. I need to know the seconds between the first time and last time. Right now I'm doing it like this:

text = text.Substring(text.IndexOf("Objects]") + 8).Trim();
string[] lines = text.Split('\n');
string[] firstLine = lines[0].Split(',');
string[] lastLine = lines[lines.Length - 1].Split(',');
int length = Convert.ToInt32(lastLine[2]) - Convert.ToInt32(firstLine[2]);
length = length / 1000;

I need to do this with thousands of 'text's though. Any optimalization or other methodes possible?

more information on the request of @Slothario:

  • the string text is the full text of 1 file
  • file type: .osu
  • average file size: 25KB
  • average characters: 30000
  • SOME OTHER TEXT - HitObjects ratio: 1 - 100
  • average amount of files to check: 500
\$\endgroup\$
7
  • 1
    \$\begingroup\$ This code is perfectly fine, I can't think of better solution for your case. Though it would be more convinient to use JSON/XML for that kind of stuff. \$\endgroup\$
    – Glitch
    May 1, 2019 at 13:23
  • \$\begingroup\$ How big is each text file? Are they relatively small? Can we get some numbers like the average size of the text files vs. the number of texts you need to read? Why do you want to optimize? What's the application? If we have answers to those questions I could probably be of more help. \$\endgroup\$
    – Slothario
    May 1, 2019 at 14:31
  • \$\begingroup\$ @Slothario All done \$\endgroup\$
    – Manuel
    May 1, 2019 at 15:01
  • \$\begingroup\$ Have you tested how long this will take on 500 files? My guess is it will be pretty fast. Why do you need speed improvements? Do you have to run it several times over and over again? \$\endgroup\$
    – Slothario
    May 1, 2019 at 15:03
  • \$\begingroup\$ This doesn't look very efficient (Substring, Trim and Split all create copies that are not really needed). With custom parsing you could skip all this copying, but will make the code much less simple. That said have you tested if this parsing is the bottleneck, usually when reading files from disk a little inefficiency in the processing won't matter too much. \$\endgroup\$
    – Knoop
    May 1, 2019 at 15:08

1 Answer 1

1
\$\begingroup\$

If you want to support an insane large file with a small hardware footprint you should use streaming. Something like

public static class Program
{
    public static void Main(string[] args)
    {
        string text =
            @"SOME OTHER TEXT
WHITE SPACES AND NEW LINES

[HitObjects]
109,192,7241,1,0,0:0:0:0:
256,192,7413,1,0,0:0:0:0:
475,192,75865,1,0,0:0:0:0:
329,192,86524,1,0,0:0:0:0:
182,192,256242,1,0,0:0:0:0:
256,192,306521,1,2,0:0:0:0:

WHITE SPACES AND NEW LINES
";

        Task.Run(async () =>
        {
            using (var reader = new StringReader(text)) //This should be streamed from a disk or network stream or similar
            {
                string line;
                var inScope = false;
                int? start = null;
                int last = 0;

                while ((line = (await reader.ReadLineAsync())) != null)
                {
                    if (inScope)
                    {
                        var values = line.Split(',');
                        if (values.Length != 6)
                            break;

                        last = int.Parse(values[2]);

                        if (!start.HasValue)
                            start = last;

                    } else if (line.StartsWith("[HitObjects]"))
                        inScope = true;
                } 
                Console.WriteLine(last - start);
            }
        });

        Console.ReadLine();
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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