【发布时间】:2013-01-13 09:00:20
【问题描述】:
我正在编写一个接受用户输入的 UI 应用程序 -
他将最多 50,000 个条目粘贴到 Textbox,我需要将其转换为 List<Uint32>(不同)
在这个过程中,我在“文本框”中显示了Distict列表(输出)。
我正在拆分文本并将其转换为 Uint32 的 Distinct 列表 然后我将列表转换为数组。
private List<UInt32> ConvertTextToList(string TextBoxText)
{
string[] TextBoxSplitted = TextBoxText.Split(new string[] { Environment.NewLine},StringSplitOptions.RemoveEmptyEntries); //Fast
var TextBoxSplittedAsList = TextBoxSplitted.ToList<string>(); //Fast
List<UInt32> lp = TextBoxSplittedAsList.ConvertAll(new Converter<string, UInt32>(element => Convert.ToUInt32(element))); //Fast
List<UInt32> uintList = lp.Distinct<UInt32>().ToList<UInt32>(); //Fast
UInt32[] uintListArray = uintList.ToArray(); //Fast
//Slow part (measured 15 sec on core2duo 2.53GHz)
StringBuilder builder = new StringBuilder();
Array.ForEach(uintListArray, x => builder.Append(x));
//Done slow part
SomeTextBox.text = builder.ToString();
return uintList;
}
首先我尝试使用 - ListOfHeliostatsText.Text = string.Join(",", uintListArray);
哪个更慢(比使用 StringBuilder 慢约 25%)
感觉我的函数设计错了,两次多次转换。
有没有办法提高这个功能的性能?
编辑: 我的错, 慢的部分是 ListOfHeliostatsText.Text = builder.ToString();
我会继续阅读答案。
【问题讨论】:
-
与您的问题没有直接关系...您不需要 both
uintList和uintListArray。由于您没有添加/删除任何元素,因此只需使用数组。 -
您确定用户输入完全正确吗?
-
我试过你说的代码很慢,运行大约10毫秒。在我的电脑上,它具有相同的处理器。如果我使数组包含 8000 万个项目而不是 50000 个,我会接近你所说的。你是如何测量时间的?
-
我的错,慢的部分在
SomeTextBox.text = builder.ToString()@BrankoDimitrijevic 在他的回答中提到。