Advertisement
Cstolworthy

IEnumerableSlowdown-Linqpad

Jul 11th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1.     IEnumerable<string> fullList = GetRandomStrings(10000);
  2.     IEnumerable<string> comparisonList = GetRandomStrings(10000);
  3.    
  4.     var inefficientTime = ProcessList_Inefficient(fullList,comparisonList);
  5.     var efficientTime = ProcessList_Efficient(fullList,comparisonList);
  6.    
  7.     var msg1 = "Inefficient took "+inefficientTime.ToString() + " milliseconds";
  8.     var msg2 = "Efficient took "+efficientTime.ToString() + " milliseconds";
  9.    
  10.     msg1.Dump();
  11.     msg2.Dump();
  12. }
  13.  
  14. IEnumerable<string> GetRandomStrings(int count)
  15. {
  16.     for(int i = 0; i < count;i++){
  17.         yield return Path.GetRandomFileName(); 
  18.     }
  19. }
  20.  
  21. long ProcessList_Inefficient(IEnumerable<string> firstList, IEnumerable<string> comparisonList){
  22.     Stopwatch sw = Stopwatch.StartNew();
  23.     firstList.ToList().RemoveAll(x=>{
  24.         if(comparisonList.Contains(x)){
  25.             return true;
  26.         }
  27.         return false;
  28.     });
  29.     return sw.ElapsedMilliseconds;
  30. }
  31.  
  32. long ProcessList_Efficient(IEnumerable<string> firstList, IEnumerable<string> comparisonList){
  33.     Stopwatch sw = Stopwatch.StartNew();
  34.     var comparisonListAsList = comparisonList.ToList();
  35.    
  36.     firstList.ToList().RemoveAll(x=>{
  37.         if(comparisonListAsList.Contains(x)){
  38.             return true;
  39.         }
  40.         return false;
  41.     });
  42.     return sw.ElapsedMilliseconds;
  43. }
  44.  
  45. class Closing{
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement