參數我有一個方法傳遞方法變量作爲代碼優化
protected List<string> WrapInTwoLines(string text, int lineLimit)
{
///There will be always two lines even first line can be empty and whole data goes to 2nd line
string[] originalLines = text.Split(new string[] { " " }, StringSplitOptions.None);
List<string> wrappedLines = new List<string>();
StringBuilder actualLine = new StringBuilder();
int index=0;
while(actualLine.Length + originalLines[index].Length < lineLimit)
{
actualLine.AppendFormat("{0} ",originalLines[index]);
index++;
}
wrappedLines.Add(actualLine.ToString());
actualLine.Clear();
while (index < originalLines.Length)
{
actualLine.AppendFormat("{0} ",originalLines[index++]);
}
wrappedLines.Add(actualLine.ToString().TrimEnd(' '));
return wrappedLines;
}
從環
for(int i=0; i< items.Count; i++)
{
length += items[i].Length + 2;
if (length > CHAR_LENGTH)
{
var list = WrapInTwoLines(items[i], CHAR_LENGTH - (length - items[i].Length + 2));
subtitlesList.Add(s.Append(list[0]).ToString());
s = new StringBuilder().AppendFormat("{0}{1}",list[1],separator);
length = s.Length;
}
else
{
s.AppendFormat("{0}{1}", items[i], separator);
}
}
我的方法創建新的每次迭代三個參考變量中被調用。我工作在這個代碼的優化和規劃實施方法如下
protected List<string> WrapInTwoLines(string[] originalLines, int lineLimit, List<string> wrappedLines, StringBuilder actualLine)
{
///There will be always two lines even first line can be empty and whole data goes to 2nd line
//string[] originalLines = text.Split(new string[] { " " }, StringSplitOptions.None);
//List<string> wrappedLines = new List<string>();
wrappedLines.Clear();
//StringBuilder actualLine = new StringBuilder();
actualLine.Clear();
//Rest remains the same
}
我認爲這將改善的代碼,但我不知道它將如何改善多少還是會提高代碼或沒有。我可以用什麼工具/技術來比較內存或速度方面的代碼優化? 另一個問題是,這是一個很好的模式來傳遞方法變量作爲參數(如上面的方法actualLine等)?