字符串构建器VS列表 [英] String Builder vs Lists
问题描述
我读在多个文件中数以百万计的行和我创建具有特定问题的所有行号的列表。例如,如果一个特定字段为空或包含无效值。
所以我的问题是什么是最有效的日期类型,保持数字列表的轨道这可能是一百万的行数的向上。将使用字符串构建器,列表或其他东西更有效率?
我的最终目标是要放出来,如特定领域的消息是空白的1-32,40 ,45,47,49-51,等于是在一个字符串生成器的情况下,我会检查以前的值,如果是只有1个我就从1将其更改为1-2,如果超过人们会用逗号分开吧,随着列表,我只想每个号码添加到列表中,然后结合那么一旦文件已经完全读出。但是,在这种情况下,我可能包含数百万个号码的多个列表。
下面是我使用结合使用字符串构建器号码列表当前代码:
字符串currentLine = sbCurrentLineNumbers.ToString();
串currentLineSub;
StringBuilder的subCurrentLine =新的StringBuilder();
StringBuilder的subCurrentLineSub =新的StringBuilder();
INT indexLastSpace = currentLine.LastIndexOf('');
INT indexLastDash = currentLine.LastIndexOf(' - ');
INT currentStringInt = 0;
如果(sbCurrentLineNumbers.Length == 0)
{
sbCurrentLineNumbers.Append(lineCount);
}
,否则如果(indexLastSpace == -1放大器;&安培; indexLastDash == -1)
{
currentStringInt = Convert.ToInt32(currentLine);
如果(currentStringInt == lineCount - 1)
sbCurrentLineNumbers.Append( - + lineCount);
,否则
{
sbCurrentLineNumbers.Append(,+ lineCount);
commaCounter ++;
}
}
,否则如果(indexLastSpace> indexLastDash)
{
currentLineSub = currentLine.Substring(indexLastSpace);
currentStringInt = Convert.ToInt32(currentLineSub);
如果(currentStringInt == lineCount - 1)
sbCurrentLineNumbers.Append( - + lineCount);
,否则
{
sbCurrentLineNumbers.Append(,+ lineCount);
commaCounter ++;
}
}
,否则如果(indexLastSpace&下; indexLastDash)
{
currentLineSub = currentLine.Substring(indexLastDash + 1);
currentStringInt = Convert.ToInt32(currentLineSub);
串charOld = currentLineSub;
串charNew = lineCount.ToString();
如果(currentStringInt == lineCount - 1)
sbCurrentLineNumbers.Replace(charOld,charNew);
,否则
{
sbCurrentLineNumbers.Append(,+ lineCount);
commaCounter ++;
}
}
我的最终目标是把出像特定字段的消息是空白的1-32,40,45,47,49-51
块引用>
如果这是最终目标,通过中间人表示会如无点
列表< INT>
- 只是去了的StringBuilder
,您将节省内存和CPU的方式。I am reading in multiple files in with millions of lines and I am creating a list of all line numbers that have a specific issue. For example if a specific field is left blank or contains an invalid value.
So my question is what would be the most efficient date type to keep track of a list of numbers that could be upwards of a million number of rows. Would using String Builder, Lists, or something else be more efficient?
My end goal is to out put a message like "Specific field is blank on 1-32, 40, 45, 47, 49-51, etc. So in the case of a String Builder, I would check the previous value and if it is is only 1 more I would change it from 1 to 1-2 and if it was more than one would separate it by a comma. With the List, I would just add each number to the list and then combine then once the file has been completely read. However in this case I could have multiple list containing millions of numbers.
Here is the current code I am using to combine a list of numbers using String Builder:
string currentLine = sbCurrentLineNumbers.ToString(); string currentLineSub; StringBuilder subCurrentLine = new StringBuilder(); StringBuilder subCurrentLineSub = new StringBuilder(); int indexLastSpace = currentLine.LastIndexOf(' '); int indexLastDash = currentLine.LastIndexOf('-'); int currentStringInt = 0; if (sbCurrentLineNumbers.Length == 0) { sbCurrentLineNumbers.Append(lineCount); } else if (indexLastSpace == -1 && indexLastDash == -1) { currentStringInt = Convert.ToInt32(currentLine); if (currentStringInt == lineCount - 1) sbCurrentLineNumbers.Append("-" + lineCount); else { sbCurrentLineNumbers.Append(", " + lineCount); commaCounter++; } } else if (indexLastSpace > indexLastDash) { currentLineSub = currentLine.Substring(indexLastSpace); currentStringInt = Convert.ToInt32(currentLineSub); if (currentStringInt == lineCount - 1) sbCurrentLineNumbers.Append("-" + lineCount); else { sbCurrentLineNumbers.Append(", " + lineCount); commaCounter++; } } else if (indexLastSpace < indexLastDash) { currentLineSub = currentLine.Substring(indexLastDash + 1); currentStringInt = Convert.ToInt32(currentLineSub); string charOld = currentLineSub; string charNew = lineCount.ToString(); if (currentStringInt == lineCount - 1) sbCurrentLineNumbers.Replace(charOld, charNew); else { sbCurrentLineNumbers.Append(", " + lineCount); commaCounter++; } }
解决方案My end goal is to out put a message like "Specific field is blank on 1-32, 40, 45, 47, 49-51
If that's the end goal, no point in going through an intermediary representation such as a
List<int>
- just go with aStringBuilder
. You will save on memory and CPU that way.这篇关于字符串构建器VS列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!