2013-12-08 57 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 
*/