C#函数的优化选项 [英] Optimization Options for a C# function

查看:86
本文介绍了C#函数的优化选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个功能,它可以工作,完全按照我的需要做,但是;功能非常慢...不是几分钟慢,但我想它可以到达那里,因为我收到的文件变得更大。我只需要一些建议来蒸汽线或优化功能来加快处理速度。这是C#/ Silverlight应用程序的一小部分。



目前文件的大小范围是:

1kb(不等等......我喜欢)



到周围



350kb(使应用程序无响应......我不喜欢)

< pre lang =c#> private void MyStupidButton_ClickFunction( object sender,RoutedEventArgs e)
{
fileContents.Text = ;
if (Application.Current.HasElevatedPermissions)
{
// 更新文件选择过程
_openFileDialog = new OpenFileDialog();
_openFileDialog.Filter = 文本文件(.txt)| * .txt;
_openFileDialog.Multiselect = false ;
List< string> list = new List< string>();

bool ? dialogResult = _openFileDialog.ShowDialog();
if (dialogResult.Value)
{
Stream fileStream = _openFileDialog.File.OpenRead();
使用(StreamReader reader = new StreamReader(fileStream))
{
string 行;

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 = 找到重复的条目;
}
else
{
duplicate.Text = 找不到重复;
}
enableClearButton();
}
}
}



非常感谢任何帮助,如果我只是需要学习耐心,那也很好。

解决方案

像其他人说的那样。

这里是合并:

  bool ? dialogResult = _openFileDialog.ShowDialog(); 
if (dialogResult.HasValue&& dialogResult.Value)
{
FilePath.Text = _openFileDialog.File.FullName;
string [] allLines = File.ReadAllLines(FilePath.Text); // 仅读取文件ONCE

fileContents.Text = string .Join( \ n,allLines );
enableVerifyButton();

if (allLines.Length > allLines.Distinct.Count)
{
duplicate.Text = 找到重复的条目;
}
else
{
duplicate.Text = 找不到重复;
}
enableClearButton();
}



这可能不是最佳应该更快。



你真的需要 fileContents.Text 控件中的内容吗?

这需要构建字符串。


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)

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.

解决方案

Like others said.
Here it''s "consolidated":

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.


这篇关于C#函数的优化选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆