四種方法把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));
}
{
//測試字符串
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
大家如果有什么好的方法,也讓我學習學習哦!

浙公網安備 33010602011771號