[Mono-dev] Trivial Enumerable Optimizations
Robert Jordan
robertj at gmx.net
Fri May 9 11:38:00 EDT 2008
Jonathan Pryor wrote:
> So is it possible to further optimize the LongCount() extension method?
> Perhaps something like this?
>
> public static long LongCount<TSource> (this IEnumerable<TSource> source)
> {
> // as before...
> ICollection<TSource> c = source as ICollection<TSource>;
> if (c != null) {
> try {
> return c.Count;
> }
> catch {
> // ignore; collection has > int.MaxValue elements,
> // so count manually below.
> }
> }
> // as before...
> }
>
> This will suffer an obvious performance penalty if the collection has
> over int.MaxValue elements (assuming .Count throws an exception when
> that happens), but as most (currently all?) collections will have less
> than int.MaxValue elements, this should be a performance win.
>
> Thoughts?
I'd check whether "source" is an array and return what
Array.GetLongLength returned.
Robert
More information about the Mono-devel-list
mailing list