-
-
Save rarous/2396976 to your computer and use it in GitHub Desktop.
ForEach extension with micro optimization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext) | |
{ | |
if (source == null) | |
{ | |
throw new ArgumentNullException("source"); | |
} | |
if (onNext == null) | |
{ | |
throw new ArgumentNullException("onNext"); | |
} | |
var array = source as TSource[]; | |
if (array != null) | |
{ | |
Array.ForEach(array, onNext); | |
return; | |
} | |
var list = source as List<TSource>; | |
if (list != null) | |
{ | |
list.ForEach(onNext); | |
return; | |
} | |
foreach (TSource current in source) | |
{ | |
onNext(current); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Navíc
IEnumerable
je ještě k tomu virtual call, což má taky svůj cost. Ale já měl specifickej případ, kdy jsem migroval spoustu dat z jednoho systému do novýho se zcala jinou strukturou a každé urychlení představovalo velké plus. Původní naivní implementace travala asi dva dny. Po různých optimalizacích (jak v návrhu tak microoptimalizace) to kleslo na pár hodin. :)