What’s the fastest way to get the length of an .net IEnumerable<T> in IronPython? The Python standard len() function doesn’t seem to work on it.
I know I can do
len(list(my_enumerable))
or
list(my_enumerable).Count
But that might be slower than necessary.
Do you know if it actually implements
IEnumerable<T>? If so, you could useEnumerable.Count(IEnumerable<T> source)(part of LINQ to Objects).I don’t know of an equivalent for the non-generic
IEnumerable– although you could implement such a method yourself easily enough. In C# it would look something like this:Note that disposing of the iterator at the end is important – but you can’t use a
usingstatement asIEnumeratoritself doesn’t implementIDisposable(unlikeIEnumerator<T>).Of course you could either use that as C# in a class library to call from IronPython, or translate the code into IronPython yourself.