有一个txt文本,我需要对他每一行进行遍历,并在匹配的地方做文本截取,
常规情况下肯定就直接用string.Substring,但一测试,耗时直接干到60000ms,也就是60s.
如下面代码中被注释掉的部分,无论是用Substring还是Split都在60s左右.(还不是每一行都要截取)
最后使用了StringBuilder.CopyTo(),直接从60000ms降到25ms
代码中其他的代码作用不用管,测试过,那些逻辑耗时不到5ms,
修改前:
for (var i = 0; i < lines.Count; i++)
{
var instance = lines[i];
if (i > 0) offset += 2;//+2是换行符
offsetsDic.Add(i, new KeyValuePair<int, int>(offset, offset += instance.LineText.Length));
var LineText = instance.LineText.Trim();
if (instance.LineText.Length > 2 && LineText[0] == '[' && LineText[1] == '@')
{
//var index = LineText.IndexOf(']');
//var funName = index > -1 ? LineText.Substring(0, index) : LineText;
//var funName = instance.LineText.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault().ToUpperInvariant();
fieldLineoffset.Add(i, funName);
}
else
{
if (fieldLineoffset.Any())
fieldLineoffset.Add(i, fieldLineoffset[i - 1]);
}
}
修改后: text 就是 lines的原文本
var sb = new StringBuilder(text);
for (var i = 0; i < lines.Count; i++)
{
var instance = lines[i];
if (i > 0) offset += 2;//+2是换行符
offsetsDic.Add(i, new KeyValuePair<int, int>(offset, offset + instance.LineText.Length));
var LineText = instance.LineText.Trim();
if (instance.LineText.Length > 2 && LineText[0] == '[' && LineText[1] == '@')
{
var funName = LineText;
var index = LineText.IndexOf(']');
if (index > -1)
{
var chars = new char[instance.LineText.Length - LineText.Length + index];
sb.CopyTo(offset + (instance.LineText.Length - LineText.Length), chars, 0, chars.Length);
funName = new string(chars);
}
fieldLineoffset.Add(i, funName);
}
else
{
if (fieldLineoffset.Any())
fieldLineoffset.Add(i, fieldLineoffset[i - 1]);
}
offset += instance.LineText.Length;
}