【问题标题】:Optimization of file read in C#C#中文件读取的优化
【发布时间】:2012-10-09 03:13:30
【问题描述】:

我需要复制一个文件,解析它的内容,删除换行符,沿着管道分割内容,然后将生成的 string[] 存储在数据库中。我的文件每个可以有 65000 条有效记录,因此性能至关重要。

以下是我目前拥有的。问题是它非常慢(处理 65000 行需要 3 小时)。我将不胜感激任何有助于改进优化这件作品的帮助,这样我的运行速度会大大加快。

public void ReadFileLinesIntoRows()
    {
        try
        {
            using (var reader = new TextFieldParser(FileName))
            {
                reader.HasFieldsEnclosedInQuotes = false;
                reader.TextFieldType = FieldType.Delimited;
                reader.SetDelimiters("|");
                String[] currentRow;
                while (!reader.EndOfData)
                {
                    try
                    {
                        currentRow = reader.ReadFields();
                        int rowcount = currentRow.Count();
                        //if it is less than what you need, pad it.  
                        if (rowcount < 190)
                        {
                            Array.Resize<string>(ref currentRow, 190);  
                            rows.Add(currentRow);
                        }
                        else
                        {
                            rows.Add(currentRow);
                        }
                    }
                    catch (MalformedLineException mex)
                    {
                        unreadlines.Add(reader.ErrorLine);//continue afterwards
                    } 
                }
                this.TotalRowCount  = rows.Count();
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

   public void cleanfilecontent(String tempfilename, Boolean? HeaderIncluded)
    {
        try
        { 
            //remove the empty lines in the file 
            using (var sr = new StreamReader(tempfilename))
            {
                // Write new file
                using (var sw = new StreamWriter(CleanedCopy))
                {
                    using (var smove = new StreamWriter(duptempfileremove))
                    {
                        string line;

                        Boolean skippedheader = false;
                        while ((line = sr.ReadLine()) != null)
                        {
                            // Look for text to remove
                            if (line.Contains("----------------------------------"))
                            { 
                                smove.Write(line);
                            }
                            else if (HeaderIncluded.HasValue && HeaderIncluded.Value==true && ! skippedheader)
                            {
                                smove.Write(line);
                                skippedheader = true;
                            }
                            else if(skippedheader)
                            {
                                // Keep lines that does not match
                                sw.WriteLine(line);
                            } 
                        }
                        smove.Flush();
                    }
                    sw.Flush();
                }
                sr.Close();
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }

    }

【问题讨论】:

  • 你的TextFieldParser是什么样的?
  • TextFieldParser 库位于 Microsoft.VisualBasic.FileIO

标签: c# data-structures


【解决方案1】:

65000 条记录并不是那么大。如果您有足够的内存,我会建议将整个文件读入内存,执行解析并构造您的行并使用批量插入将数据提交到数据库。这将是最快的!我怀疑您 3 个小时的大部分时间都花在了一次将数据库记录插入数据库中。

【讨论】:

  • 这就是我现在正在做的工作,它真的很慢。我正在尝试寻找优化读取和解析方面的方法,如果可能的话,也许还有数据库方面。
  • 请注意,在 EF 或数据适配器中调用单个提交/提交方法并不等同于单个批量插入。在幕后,这通常会作为多个单个插入语句执行,这会影响您的性能。看看这个codeproject.com/Articles/354094/…
  • 您应该至少调试并找出瓶颈所在。是在加载/读取文件时,还是在提交记录时
  • 每条记录的提交似乎都以相同的速度进行,并且从处理将数据移植到各个表的存储过程的调试中,我认为这不是真正的问题所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2016-06-08
相关资源
最近更新 更多