最近看到一个题目,要把一个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));
}
{
// 测试字符串
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
大家如果有什么好的方法,也让我学习学习哦!