Click here to Skip to main content
15,888,018 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
So I have this function and it works and does exactly what I need it to do, however; the function is extremely slow...not minutes slow but, I imagine it could get there as the files I receive get larger. I just need a few suggestions to steam line or optimize the function to process just a little faster. This is a small part a C# / Silverlight application.

The size of the files currently range from:
1kb (no wait...me likey)

to around

350kb (renders application non-responsive...me no likey)
C#
private void MyStupidButton_ClickFunction(object sender, RoutedEventArgs e)
        {
            fileContents.Text = "";
            if (Application.Current.HasElevatedPermissions)
            {
                //UPDATE FILE SELECTION PROCESS
                _openFileDialog = new OpenFileDialog();
                _openFileDialog.Filter = "Text Files (.txt)|*.txt";
                _openFileDialog.Multiselect = false;
                List<string> list = new List<string>();

                bool? dialogResult = _openFileDialog.ShowDialog();
                if (dialogResult.Value)
                {
                    Stream fileStream = _openFileDialog.File.OpenRead();
                    using (StreamReader reader = new StreamReader(fileStream))
                    {
                        string line;
                        
                        while((line = reader.ReadLine()) != null)
                        {
                            list.Add(line);
                            fileContents.Text += line.ToString() + "\n";
                        }

                        FilePath.Text = _openFileDialog.File.FullName;
                        enableVerifyButton();
                    }
                    fileStream.Close();
                    var distinctLines = File.ReadLines(FilePath.Text).Distinct().Count();
                    var hasDuplicates = File.ReadLines(FilePath.Text).Count();

                    if (hasDuplicates > distinctLines)
                    {
                        duplicate.Text = "Duplicate Entries Found";
                    }
                    else
                    {
                        duplicate.Text = "No Duplicates Found";
                    }
                    enableClearButton();
                }
            }
        }

Any help is greatly appreciated and if I just need to learn patience, that's fine too.
Posted
Comments
Sergey Alexandrovich Kryukov 2-Apr-13 14:35pm    
Help with what? This is not a question.
OK, you know what the function should do, not please tell us about it.
Rename "MyStupidButton" to "MyCleverFunction" :-)
—SA
Kenneth Haugland 2-Apr-13 14:53pm    
How about btnMeNoLikey? :laugh:
Sergey Alexandrovich Kryukov 2-Apr-13 16:44pm    
That's the sure way to fail the project.
I used to allow myself to mentor people with the following: "Most important thing is a project is its name; should you choose a bad name, everything goes wrong" :-)
—SA
Kenneth Haugland 2-Apr-13 14:37pm    
If you are just going to read a text fil why not use ReadAllLines instead? It retunrs a list of string.
[no name] 2-Apr-13 15:09pm    
File IO is naturally slow. The only way you are going to optimize anything is to profile your code and see where the actual slowdown is.

1 solution

Like others said.
Here it's "consolidated":
C#
bool? dialogResult = _openFileDialog.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value)
{
  FilePath.Text = _openFileDialog.File.FullName;
  string[] allLines = File.ReadAllLines(FilePath.Text);  // read the file only ONCE

  fileContents.Text = string.Join("\n", allLines);
  enableVerifyButton();
 
  if (allLines.Length > allLines.Distinct.Count)
  {
    duplicate.Text = "Duplicate Entries Found";
  }
  else
  {
    duplicate.Text = "No Duplicates Found";
  }
  enableClearButton();
}

This may not be optimal but it ought to be faster.

Do you really need the contents in the fileContents.Text control?
This requires building the big string.
 
Share this answer
 
Comments
JustDaveL 3-Apr-13 8:45am    
Since this is a new function I do need the fileContents.Text control for now. After I have my formatting and logic in a more solid state the control will go away. It is for testing purposes right now. I cannot use Console output for testing so I have to incorporate controls in the UI to validate my work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900