請高手指教高效的Split函數

    /**C++實現*/   

//String繼承於std::string  
      
//我的Split代碼,vector操作很慢啊。 
                
typedef vector
<String> Strings;  
 
Strings Split(
char* by,bool havenull=true)  
{   
    Strings strs;    
    typedef unsigned 
int UINT;   
    UINT bylen
=strlen(by);   
    UINT start
=0;   
    UINT pos
=0;   
    
char* head=(char*)this->c_str();   
    
if(havenull)   
    {   
        
while((pos=this->find(by,start))!=-1)   
        {   
            
*(head+pos)=0;   
            strs.push_back(head
+start);   
            
*(head+pos)=*by;   
            start
=pos+bylen;   
        };   
    }   
    
else   
    {   
        
while((pos=this->find(by,start))!=-1)   
        {   
            
if(pos-start>0)                                       
            {   
                
*(head+pos)=0;   
                strs.push_back(head
+start);   
                
*(head+pos)=*by;   
            };   
            start
=pos+bylen;   
        };   
    };   
    
if(start<this->size())   
    {   
        strs.push_back(
this->substr(start,this->size()-start));   
    };   
    
return strs;   
};   
//測試代碼:   
void main()   
{   
    String s;   
    Strings ok;   
    s
="/c:;lihdaofhdo/al;jdf;lkajdf/adlfkadfj;ajdqwer/asdfja;ldsjqwerq/difjtt.cpp//";   
    
for(int j=0;j<1000000;j++)   
        ok
=s.Split("/",false);   
    
for(int i=0;i<ok.size();i++)   
    {   
        cout
<<ok[i]<<endl;   
    };   
};   
           
    
/**C#實現的代碼*/   
    
using System;   
    
using System.Collections.Generic;   
    
using System.Text;   
       
    
namespace ConsoleApplication1   
    {   
        
class Program   
        {   
            
static void Main(string[] args)   
            {   
                String a 
="/c:;lihdaofhdo/al;jdf;lkajdf/adlfkadfj;ajdqwer/asdfja;ldsjqwerq/difjtt.cpp//";   
                
string s = "/";   
                
char[] by = s.ToCharArray();   
                String [] strs;   
                Console.ReadKey();   
               strs 
= a.Split(by);   
               
for (int j = 0; j < 1000000; j++)   
                {   
                   strs 
= a.Split(by);   
               };   
               
for (int i = 0; i < strs.Length; i++)   
               {   
                   Console.WriteLine(strs[i]);   
               };   
                Console.ReadKey();   
           }   
       }   
    }   

//測試機器CPU:AMD64 3000+ ,操作系統Windows XPSP2。   
//都執行字符串切分一百萬次,這時C#的不到一秒完成,C++打開優化要六秒的樣子。
//哪位大俠知道高效的C++下Split代碼?嵌入彙編完成應該會快點。

發佈了45 篇原創文章 · 獲贊 3 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章