• 四种方法把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<stringint>(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

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

  • 相关阅读:
    C#读取Excel文档
    用DataSet方式更新数据库表
    using(){},Close(),Dispose()的区别
    ADO.NET连接数据库的两种方式
    DateUtils
    GoF的23个经典设计模式
    react中需要用到【深度复制】的问题
    css区分ie8/ie9/ie10/ie11 chrome firefox的代码
    【个人学习笔记】走近H5
    【React入门实例(运行于浏览器duan)】
  • 原文地址:https://www.cnblogs.com/artwl/p/1866243.html
Copyright © 2020-2023  润新知