Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anyway to optimize this one bc, it takes forever [closed]

Tags:

performance

c#

I have written some code which should store some strings out of a string-array for me line by line in a file, sadly I have to do this for about 200k strings in an array and which takes me forever...

foreach (var file in fileArray) {

    //    Console.WriteLine(file);

        string sub = file.Substring(49,file.Length - 49);
        using (StreamWriter sw = File.AppendText(folder + ".tfcs"))
        {
            sw.WriteLine(sub);   
        }
        indexfiles++;
        Console.Clear();
        Console.WriteLine("Scanning Folder:");
        Console.WriteLine(folder);
        Console.WriteLine("Filesfound:" + fileArray.Length);
        Console.WriteLine("Fileswritten to Database:" + indexfiles);
        }

}

Is there any way to improve this?

P.S. I have get the progress for the amount of strings already written to the file...

like image 566
max müller Avatar asked Nov 30 '22 21:11

max müller


1 Answers

You're opening and closing the file on every iteration of the loop. That's almost certainly the problem, or at least part of it.

Instead of that, open it once:

using (var writer = File.AppendText(folder + ".tfcs"))
{
    foreach (var file in fileArray)
    {
        writer.WriteLine(file.Substring(49, file.Length - 49));
        indexFiles++;
    }
}
Console.Clear();
Console.WriteLine("Scanning Folder:");
Console.WriteLine(folder);
Console.WriteLine("Files found:" + fileArray.Length);
Console.WriteLine("Files written to Database:" + indexFiles);

I've also moved the console output to the end - clearing the console and writing to it on every iteration is almost certainly slowing things down very significantly too. If you need some sort of progress report as you go, I'd do it every 100 lines or something like that.

like image 137
Jon Skeet Avatar answered Dec 09 '22 21:12

Jon Skeet