Cache for loop lengths

The `length`s never change in the course of their respective functions and so caching them is an appropriate optimization
Merge pull request #42 from acconrad/patch-2

Cache for loop lengths