四种方法把string[]转换为int[]

      最近看到一个题目,要把一个string[]转换为int[](当然,string[]中存的都是数字),然后写了个小程序玩儿了一下,目前用了四种方法,但方法的优劣不是那么明显:

 

         static   void  Main( string [] args)
        {
            
// 测试字符串
            StringBuilder test;
            
string [] str;
            
for  ( int  j  =   0 ; j  <   6 ; j ++ )
            {
                test 
=   new  StringBuilder();
                
for  ( int  i  =   1 ; i  <=  ( int )Math.Pow( 10 ,j + 1 ); i ++ )
                {
                    test.Append(i 
+   " , " );
                }
                test.Append(
" 1 " );
                Console.WriteLine(
" 元素个数为 " + ( int )Math.Pow( 10 , j  +   1 ) + " : " );

                str 
=  test.ToString().Split( ' , ' );
                StringToInt(str);
                StringToIntByLinq(str);
                StringToIntByArr(str);
                StringToIntByList(str);
                Console.WriteLine();
            }

            Console.Read();
        }

        
// 最简单的,用循环
         public   static   void  StringToInt( string [] str)
        {
            
long  t1  =  DateTime.Now.Ticks;
            
int [] intArr  =   new   int [str.Length];
            
for  ( int  i  =   0 ; i  <  str.Length;i ++  )
            {
                intArr[i] 
= Convert.ToInt32(str[i]);
            }
            Console.WriteLine(
" Loop Cost Time: "   +  (DateTime.Now.Ticks  -  t1));
        }

        
// 用Linq
         public   static   void  StringToIntByLinq( string [] str)
        {
            
long  t1  =  DateTime.Now.Ticks;
            
int [] intArr  =  str.Select(o  =>  Convert.ToInt32(o)).ToArray < int > ();
            Console.WriteLine(
" Linq Cost Time: "   +  (DateTime.Now.Ticks  -  t1));
        }

        
// 用.NET中的数组转换方法
         public   static   void  StringToIntByArr( string [] str)
        {
            
long  t1  =  DateTime.Now.Ticks;
            
int [] intArr  =  Array.ConvertAll < string int > (str, Convert.ToInt32);
            Console.WriteLine(
" Arr  Cost Time: "   +  (DateTime.Now.Ticks  -  t1));
        }

        
// 用泛型
         public   static   void  StringToIntByList( string [] str)
        {
            
long  t1  =  DateTime.Now.Ticks;
            
int [] intList  =  str.ToList < string > ().ConvertAll < int > (Convert.ToInt32).ToArray < int > ();
            Console.WriteLine(
" List Cost Time: "   +  (DateTime.Now.Ticks  -  t1));
        }

 

运行后效果为:

元素个数为10:
Loop Cost Time:0
Linq Cost Time:0
Arr  Cost Time:0
List Cost Time:0

元素个数为100:
Loop Cost Time:0
Linq Cost Time:0
Arr  Cost Time:0
List Cost Time:0

元素个数为1000:
Loop Cost Time:0
Linq Cost Time:0
Arr  Cost Time:0
List Cost Time:0

元素个数为10000:
Loop Cost Time:0
Linq Cost Time:156000
Arr  Cost Time:0
List Cost Time:0

元素个数为100000:
Loop Cost Time:468001
Linq Cost Time:468001
Arr  Cost Time:468001
List Cost Time:312000

元素个数为1000000:
Loop Cost Time:2496004
Linq Cost Time:2808005
Arr  Cost Time:2184004
List Cost Time:2340004

       大家如果有什么好的方法,也让我学习学习哦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值