测试字符串长度364544,编译成Release版本运行10次,取第10次作为统计结果。
统计结果如下:
GetGBLengthA() Run Time: 2.252 毫秒
GetGBLengthB() Run Time: 0.871 毫秒
GetGBLengthC() Run Time: 2.127 毫秒
1
.GetGBLength()
/**/ /**/ /**/ /// <summary>
/// 菜菜写的
/// </summary>
private static int GetGBLengthC( string strData)
... {
int iLen = 0;
if( strData!=null || strData!="")
...{
try
...{
iLen = System.Text.Encoding.Default.GetByteCount(strData);
}
catch...{}
}
return iLen;
}
2 .GetGBLengthA()
/**/ /**/ /**/ /// <summary>
/// 风@(((((
/// </summary>
private static int GetGBLengthA( string strData)
... {
System.Text.Encoding encoder5 = System.Text.Encoding.GetEncoding("GB2312");
return encoder5.GetByteCount(strData);
}
3 .GetGBLengthB /**/ /**/ /**/ /// <summary>
/// 活靶子.Net
/// </summary>
private static int GetGBLengthB( string Str)
... {
int intResult = 0;
foreach (char Char in Str)
...{
if ((int)Char > 127)
intResult = 2;
else
intResult ;
}
return intResult;
}
/**/ /**/ /**/ /// <summary>
/// 菜菜写的
/// </summary>
private static int GetGBLengthC( string strData)
... {
int iLen = 0;
if( strData!=null || strData!="")
...{
try
...{
iLen = System.Text.Encoding.Default.GetByteCount(strData);
}
catch...{}
}
return iLen;
}
2 .GetGBLengthA()
/**/ /**/ /**/ /// <summary>
/// 风@(((((
/// </summary>
private static int GetGBLengthA( string strData)
... {
System.Text.Encoding encoder5 = System.Text.Encoding.GetEncoding("GB2312");
return encoder5.GetByteCount(strData);
}
3 .GetGBLengthB /**/ /**/ /**/ /// <summary>
/// 活靶子.Net
/// </summary>
private static int GetGBLengthB( string Str)
... {
int intResult = 0;
foreach (char Char in Str)
...{
if ((int)Char > 127)
intResult = 2;
else
intResult ;
}
return intResult;
}