2013-12-08 60 views
-2

在VB6中是否有一个现代.NET相当于TextFieldParser类?性能比与一个简单的代码String.Split()TextFieldParser在.NET中等效吗?

+1

你是什么意思性能低很多?你有基准吗? –

+10

我不明白..'TextFieldParser'是.NET库的一部分 – meda

+0

@David L yeas首先我在这里搜索了一个基准http://www.dotnetperls.com/textfieldparser,之后我自己测试了一下,结果,要低很多倍。 – ElektroStudios

回答

1

我比较性能低了不少:https://gist.github.com/Ruszrok/7861319

我使用的输入文件,约1 000 000条记录与空格隔开。我试了五次实验。

  • String.Split平均时间:291毫秒
  • Microsoft.VisualBasic.FileIO.TextFieldParser平均时间:15843毫秒

可以使用Microsoft.VisualBasic.FileIO.TextFieldParser类。参考Microsoft.VisualBasic。要点示例。

+25

然而string.split不会考虑整个CSV规范 - 例如逗号和嵌入字段中的引号不会被搞砸一个string.split方法 - 所以你可以使用拆分方法 - 但只有它你确定你的数据会是什么样的 – WantToBeAnonomous

+7

你的'string.Split'测试代码只读取一行并用空格分隔,而'TextFieldParser'必须读取并解析整个文件。当然,这需要几毫秒的时间。但整个比较是无稽之谈。使用正确的工具进行工作。如果你有真正的csv数据(如果字段用引号括起来的话),'TextFieldParser'比简单的'String.Split'强大得多。 –

0

这是我的解决方案:

public class TextFieldParser 
    { 
     enum FieldType { FixedWidth, Delimited }; 

     public enum CompleteElements 
     { 
      /// <summary> 
      /// Returns as many elements as fileWidths input be 
      /// </summary> 
      AllElements, 
      /// <summary> 
      /// Only returns elements who have not null values 
      /// </summary> 
      OnlyValues 
     }; 

     int[] m_fieldWidths; 
     string m_line; 
     List<string> m_results; 
     int m_lineWidth; 
     public CompleteElements m_CompleteElements; 

     public TextFieldParser(string line) 
     { 
      m_line = line; 
      m_lineWidth = m_line.Length; 
      m_results = new List<string>(); 
      m_CompleteElements = CompleteElements.OnlyValues; 
     } 

     public void SetCompleteElements(CompleteElements value) 
     { 
      m_CompleteElements = value; 
     } 

     public void SetFieldWidths(params int[] fileWidths) 
     { 
      m_fieldWidths = fileWidths; 
     } 

     public string[] ReadFields() 
     { 
      int pivot = 0; 
      m_results = new List<string>(); 

      for (int x = 0; x < m_fieldWidths.Length; x++) 
      { 
       if(pivot + m_fieldWidths[x] <= m_lineWidth) 
       { 
        m_results.Add(m_line.Substring(pivot, m_fieldWidths[x]));      
       } 
       else 
       { 
        if (m_CompleteElements == CompleteElements.AllElements) 
        { 
         m_results.Add(null); 
        } 

        break; 
       } 
       pivot += m_fieldWidths[x]; 
      } 
      return m_results.ToArray(); 
     } 
    } 

一个简单的会话:

string line = "123456789"; 
TextFieldParser parser = new TextFieldParser(line); 
parser.SetFieldWidths(1, 2, 3, 4, 5, 6, 7, 8); 

string[] resultOnlyValues = parser.ReadFields(); 
/* 
results: 
resultOnlyValues[0] : "1" 
resultOnlyValues[1] : "23" 
resultOnlyValues[2] : "456" 
resultOnlyValues[3] : "7890" 
resultOnlyValues[4] : "12345" 
resultOnlyValues[5] : "678901" 
resultOnlyValues[6] : "2345678" 
*/ 
parser.SetCompleteElements(TextFieldParser.CompleteElements.AllElements); 
string[] resultAllElement = parser.ReadFields(); 
/* 
results: 
resultAllElement[0] : "1" 
resultAllElement[1] : "23" 
resultAllElement[2] : "456" 
resultAllElement[3] : "7890" 
resultAllElement[4] : "12345" 
resultAllElement[5] : "678901" 
resultAllElement[6] : "2345678" 
resultAllElement[7] : null 
*/