none
这个程序如何提高速度 RRS feed

  • 问题

  •    await Task.Factory.StartNew(() =>
                    {
                        for (int i = 0; i < listc.Count() / cc; i++)
                        {
                            var cv = listc.Skip(i * cc).Take(cc);
                            list1.AddRange(cv.Skip(1).Take(100));
                            list2.AddRange(cv.Skip(101).Take(100));
                            list3.AddRange(cv.Skip(201).Take(100));
                        }
                    },TaskCreationOptions.LongRunning);
    lisc是一个很大的list《double》集合,

    please verify my account

    2017年5月11日 5:29

全部回复

  • 目测没有看出问题,考虑用.net performance观察一下,是哪句代码慢了。


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2017年5月12日 0:47
  • Hi,

    感谢你在MSDN论坛发帖。

    这段代码看不出来什么问题,但是我用HashSet和list做了一个比较。当数据多余一定成都以后,HashSet的效率会高于list,所以你的数据要是多,我建议你使用HashSet。

    下面是测试代码;

      static void Main(string[] args)
            {
                int times = 10000000;
    
    
                for (int listSize = 1; listSize < 10; listSize++)
                {
                    List<string> list = new List<string>();
                    HashSet<string> hashset = new HashSet<string>();
    
                    for (int i = 0; i < listSize; i++)
                    {
                        list.Add("string" + i.ToString());
                        hashset.Add("string" + i.ToString());
                    }
    
                    Stopwatch timer = new Stopwatch();
                    timer.Start();
                    for (int i = 0; i < times; i++)
                    {
                        list.Remove("string0");
                        list.Add("string0");
                    }
                    timer.Stop();
                    Console.WriteLine(listSize.ToString() + " item LIST strs time: " + timer.ElapsedMilliseconds.ToString() + "ms");
    
    
                    timer = new Stopwatch();
                    timer.Start();
                    for (int i = 0; i < times; i++)
                    {
                        hashset.Remove("string0");
                        hashset.Add("string0");
                    }
                    timer.Stop();
                    Console.WriteLine(listSize.ToString() + " item HASHSET strs time: " + timer.ElapsedMilliseconds.ToString() + "ms");
                    Console.WriteLine();
                }
    
    
                for (int listSize = 1; listSize < 50; listSize += 3)
                {
                    List<object> list = new List<object>();
                    HashSet<object> hashset = new HashSet<object>();
    
                    for (int i = 0; i < listSize; i++)
                    {
                        list.Add(new object());
                        hashset.Add(new object());
                    }
    
                    object objToAddRem = list[0];
    
                    Stopwatch timer = new Stopwatch();
                    timer.Start();
                    for (int i = 0; i < times; i++)
                    {
                        list.Remove(objToAddRem);
                        list.Add(objToAddRem);
                    }
                    timer.Stop();
                    Console.WriteLine(listSize.ToString() + " item LIST objs time: " + timer.ElapsedMilliseconds.ToString() + "ms");
    
    
    
                    timer = new Stopwatch();
                    timer.Start();
                    for (int i = 0; i < times; i++)
                    {
                        hashset.Remove(objToAddRem);
                        hashset.Add(objToAddRem);
                    }
                    timer.Stop();
                    Console.WriteLine(listSize.ToString() + " item HASHSET objs time: " + timer.ElapsedMilliseconds.ToString() + "ms");
                    Console.WriteLine();
                }
    
                Console.ReadLine();
            }

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年5月12日 2:40
    版主