Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Foreach vs AddRange
Message
From
12/08/2009 09:19:13
 
 
To
12/08/2009 08:38:58
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01417359
Message ID:
01417526
Views:
42
>>Adding an extension method ( say AddCollection) is a bad idea since it accepts an argument of IEnumerable(T).
>>
>>using items.Count() is slow - I guess it uses a foreach loop to count
>>
>>
>>// don't do this at home
>>
>>public static void AddCollection<T>(this List<T> list, IEnumerable<T> items)
>>		{
>>
>>			if (items.Count() <= Tuning.AddRangeMinimumItems) // 128
>>			{
>>				foreach (T item in items)
>>					list.Add(item);
>>			}
>>			else
>>			{
>>				list.AddRange(items);
>>			}
>>		}
>>
>>
>>An extension method that takes a whole array is a better idea - since array.Length is faster
>
>Gregory,
>Sorry I didn't read the whole thread to see your purpose and solutions so far, could you use and try Linq Concat()? ie:
>
>	public static void AddCollection<T>(this List<T> list, IEnumerable<T> items)
>	{
>			list.Concat( items );
>	}
>
>
>Update: I made a short test with this using your original test and probably I am doing something wrong, results are unbelievably fast:
>
>00:00:05.5402003 // UseAddRange
>00:00:01.9798588 // UseForeach
>00:00:00.3055594 // UseConcat
>
>Thinking F# power and sequence processing behind Linq that result might be perfectly right. Here is my UseConcat BTW:
>
>
>       static void UseConcat()
>        {
>            int[] buf = new int[Size];
>            List<int> list = new List<int>();
>            for (int i = nTimes; --i != 0; )
>            {
>                list.AddCollection(buf);
>                list.Clear();
>            }
>
>        }
>
>Cetin

Cetin,

I started testing and wow concat is very fast.
Alas. It just returns another Enumerator which plugs the two enumerators together
Return Value
Type: System.Collections.Generic..::.IEnumerable <(Of <(TSource>)>)
An IEnumerable<(Of <(T>)>) that contains the concatenated elements of the two input sequences.

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.
It's easily seen in the debugger
			int[] pp = new int[Size];

			List<int> list = new List<int>();

			var tt = list.Concat(pp);
		i	0	int
+		pp	{int[256]}	int[]
+		list	Count = 0	System.Collections.Generic.List<int>
-		tt	{System.Linq.Enumerable.ConcatIterator<int>}	System.Collections.Generic.IEnumerable<int> {System.Linq.Enumerable.ConcatIterator<int>}
		first	null	System.Collections.Generic.IEnumerable<int>
		second	null	System.Collections.Generic.IEnumerable<int>
+		Non-Public members		
+		Results View	Expanding the Results View will enumerate the IEnumerable	
It's the about the same as my Queue.Reverse() - remember ?

Thanks,
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform