0

我正在阅读数百万行的多个文件,并且正在创建一个包含特定问题的所有行号的列表。例如,如果特定字段留空或包含无效值。

所以我的问题是什么是最有效的日期类型来跟踪可能超过一百万行的数字列表。使用字符串生成器、列表或其他东西会更有效吗?

我的最终目标是发出一条消息,例如“特定字段在 1-32、40、45、47、49-51 等处为空白。因此,对于字符串生成器,我将检查先前的值,如果它is 只是多了 1 我会将其从 1 更改为 1-2 如果超过一个将用逗号分隔它使用列表,我只需将每个数字添加到列表中,然后在文件具有已完全阅读。但是在这种情况下,我可以有多个包含数百万个数字的列表。

这是我使用 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++;
    }
}   
4

5 回答 5

5

我的最终目标是发出一条消息,例如“1-32、40、45、47、49-51 上的特定字段为空白

如果这是最终目标,那么通过诸如 a 之类的中间表示毫无意义List<int>- 只需使用StringBuilder. 这样可以节省内存和 CPU。

于 2012-04-09T16:40:20.293 回答
3

StringBuilder 服务于您的目的,所以坚持下去,如果您需要行号,您可以轻松更改代码。

于 2012-04-09T16:43:43.093 回答
3

取决于您如何/想要分解代码。

鉴于您正在按行顺序阅读它,因此不确定您是否需要一个列表。您当前所需的输出意味着在完全扫描文件之前您无法输出任何内容。文件的大小表明,一次性分析阶段也是一个好主意,因为您将使用缓冲输入而不是将整个内容读入内存。

我很想用枚举来描述这个问题,例如字段???是空白的,然后将其用作字符串构建器字典的键。

无论如何,作为第一个想法

于 2012-04-09T16:51:38.097 回答
2

你的输出应该是人类可读的吗?如果是这样,那么在您的数据结构出现任何性能/内存问题之前您就会达到合理阅读的极限。使用对您来说最容易使用的东西。

如果输出应该是机器可读的,那么该输出可能会建议适当的数据结构。

于 2012-04-09T17:09:27.810 回答
2

正如其他人指出的那样,我可能会使用StringBuilder. 列表可能需要多次调整大小;的新实现StringBuilder不必调整大小。

于 2012-04-09T17:14:26.517 回答