测试了3种获取中英文混合字符串长度的方法,看来还是使用循环来的快

博客展示了三种获取中英文混合字符串长度的方法,分别是GetGBLengthC、GetGBLengthA和GetGBLengthB。通过对长字符串进行多次测试,使用StopWatch记录各方法运行时间,以比较它们的性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.cnblogs.com/cest/archive/2004/12/01/71297.html

分别写了3个函数:
1.GetGBLength()
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// 菜菜写的
ExpandedBlockEnd.gif
/// </summary>

None.gif private   static   int  GetGBLengthC( string  strData)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {            
InBlock.gif    
int iLen = 0;
InBlock.gif    
if( strData!=null || strData!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            iLen 
= System.Text.Encoding.Default.GetByteCount(strData);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
catchdot.gif{}
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return iLen;
ExpandedBlockEnd.gif}
  

2.GetGBLengthA()
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// 风@((((( 
ExpandedBlockEnd.gif
/// </summary>

None.gif private   static   int  GetGBLengthA( string  strData) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    System.Text.Encoding encoder5 
= System.Text.Encoding.GetEncoding("GB2312"); 
InBlock.gif    
return encoder5.GetByteCount(strData); 
ExpandedBlockEnd.gif}


3.GetGBLengthB
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// 活靶子.Net
ExpandedBlockEnd.gif
/// </summary>

None.gif private   static   int  GetGBLengthB( string  Str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int intResult = 0;
InBlock.gif    
foreach (char Char in Str)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ((int)Char > 127)
InBlock.gif            intResult 
+= 2;
InBlock.gif        
else
InBlock.gif            intResult 
++;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return intResult;
ExpandedBlockEnd.gif}

测试主程序:
None.gif static   void  Main( string [] args)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    StopWatch sw 
= new StopWatch();
InBlock.gif    
string strData = "01234567890阿发端发;立法局阿;领导法拉第发端夫君阿;地方阿斗发狂三角枫哈萨克毒发啊发狂挥发可回到发阿克苏多幅阿抗倒伏阿括弧发狂复活卡哈喀什发咖啡阿卡罚款罚款发狂撒发狂撒发狂撒多幅阿卡大饭卡大发狂撒毒发阔的饭卡大饭卡大饭卡大饭卡大发狂撒多幅阿克苏大罚款毒发阔的发咔哒复活卡多幅卡多幅卡撒多幅卡撒多幅哈克三点会发阿抗倒伏阿克苏多幅阿卡饭卡大akdfhaksdjfha kdfa ";
InBlock.gif    
forint i=0; i<10; i++ )
InBlock.gif        strData 
+=strData;
InBlock.gif    GC.Collect();
InBlock.gif
InBlock.gif    sw.Reset();
InBlock.gif    GetGBLengthA(strData);
InBlock.gif    Console.WriteLine(
"GetGBLengthA() Run Time: " + (sw.Peek() / (float)1000).ToString() + " 毫秒");
InBlock.gif
InBlock.gif    sw.Reset();
InBlock.gif    GetGBLengthB(strData);
InBlock.gif    Console.WriteLine(
"GetGBLengthB() Run Time: " + (sw.Peek() / (float)1000).ToString() + " 毫秒");
InBlock.gif    
InBlock.gif    sw.Reset();
InBlock.gif    GetGBLengthC(strData);
InBlock.gif    Console.WriteLine(
"GetGBLengthC() Run Time: " + (sw.Peek() / (float)1000).ToString() + " 毫秒");
ExpandedBlockEnd.gif}

None.gif

测试字符串长度364544,编译成Release版本运行10次,取第10次作为统计结果。
统计结果如下:
GetGBLengthA() Run Time: 2.252 毫秒
GetGBLengthB() Run Time: 0.871 毫秒
GetGBLengthC() Run Time: 2.127 毫秒

不知道这样测试十分能够体现真实性?

在这里感谢 风@(((((  和 活靶子.Net

转载于:https://www.cnblogs.com/cest/archive/2004/12/02/72093.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值