∈鱼杆 ---我的鱼塘

执着,坚定,友爱,勇敢(www.pumaboyd.com)Live Message

导航

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

Posted on 2007-08-29 19:20  ∈鱼杆  阅读(198)  评论(0编辑  收藏  举报

因为进来公司要给其他公司提供数据,数据中有字符要替换。有两种替换方法
 
代码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