Performance of Skip (and similar functions, like Take)

In Jon Skeet’s excellent tutorial re-implementing Linq, he discusses (briefly) that very question:

Although most of these operations can’t be sensibly optimized, it
would make sense to optimize Skip when the source implements IList.
We can skip the skipping, so to speak, and go straight to the
appropriate index. This wouldn’t spot the case where the source was
modified between iterations, which may be one reason it’s not
implemented in the framework as far as I’m aware.

That seems like a reasonable reason to hold off on that optimization, but I agree that for specific cases, it may be worthwhile to make that optimization if you can guarantee your source can’t/won’t be modified.

Leave a Comment