list.Take(100).ToList() vs. list.GetRange(0,100)

前端 未结 5 1716
臣服心动
臣服心动 2021-02-19 11:57
List attendees = new List();
foreach ...
// Error: \"There are too many target users in the email address array\"
// for more tha         


        
5条回答
  •  盖世英雄少女心
    2021-02-19 12:34

    It is much more faster. Check this out:

    var list = Enumerable.Range(0, 1000).ToList();
    
    var stopwatch = new Stopwatch();
    
    stopwatch.Start();
    
    for(var i=0; i<1000000; i++)
    {
        var c = list.GetRange(0, 100);
    }
    
    Console.WriteLine(stopwatch.Elapsed);
    
    stopwatch.Restart();
    
    for (var i = 0; i < 1000000; i++)
    {
         var c = list.Take(100).ToList();
    }
    
    Console.WriteLine(stopwatch.Elapsed);
    

    Elapsed time:

    List.GetRange() : 0.149 s

    List.Take().ToList() : 3.625 s

提交回复
热议问题