/*
*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代码?嵌入汇编完成应该会快点。
// 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代码?嵌入汇编完成应该会快点。