替换的效率测试(MS VS MY)

因为进来公司要给其他公司提供数据,数据中有字符要替换。有两种替换方法
 
代码1:

public   static   void  Main()
    {

            
string  input1  =   "" ;

            
            StringBuilder sb1 
=   new  StringBuilder();
            
for ( int  i  = 0  ; i  <   50000 ; i ++ )
            {
                sb1.Append(
" wo\r " );
            }
            
            input1 
=  sb1.ToString();
            
            WL(
" sb Test Date is complate.. " );
            
            Stopwatch sw1 
=   new  Stopwatch();
        
            
            
// char
            sw1.Start();
            StringBuilder sb 
=   new  StringBuilder();
            
if  (input1  !=  String.Empty) 
            {
                
for  ( int  i  =   0 ; i  <  input1.Length; i ++
                {
                    
switch  (input1[i]) 
                    {
                        
case  ( char ) 13 :
                            sb.Append(
"   " );
                            
break ;
                        
case  ( char ) 10 :
                            sb.Append(
"   " );
                            
break ;
                        
default :
                            sb.Append(input1[i]);
                            
break ;
                    }
                }
                input1 
=   sb.ToString();
            }
            sw1.Stop();
            
            
            WL(sw1.ElapsedMilliseconds
+   " 毫秒 " );
        
            
        RL();
    }


代码2:

public   static   void  Main()
    {
    
        
string  input1  =   "" ;

            
            StringBuilder sb1 
=   new  StringBuilder();
            
for ( int  i  = 0  ; i  <   50000 ; i ++ )
            {
                sb1.Append(
" wo\r " );
            }
            
            input1 
=  sb1.ToString();
            
            WL(
" replace Test Date is complate.. " );
            
            Stopwatch sw1 
=   new  Stopwatch();
        
            
            
// char
            sw1.Start();
            input1 
=  input1.Replace( " \r " , "   " );
            input1 
=  input1.Replace( " \n " , "   " );
            sw1.Stop();
            
            
            WL(sw1.ElapsedMilliseconds
+   " 毫秒 " );
        
        
        RL();
    }



Figure1: 6s
Figure2:3s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值