gimite/google-drive-ruby — pull request #49 by jhecking (2013-03-13 10:52:35+00:00)
Title: Memoize Worksheet#num_cols and num_rows

Optimize iterating over all ListRows in a Worksheet by memoizing the
`Worksheet#num_cols` and `Worksheet#num_rows` methods. For a worksheet
with about 1200 rows with 36 columns each this reduced the run-time of
the following code snippet from more than 1900 seconds to less than 3
seconds:

bookings = worksheet.list
pending = bookings.select{|row| row['Order ID'].present? and row['Status'] == 'pending'}

I used ruby-prof to trace where this time was spent. The results pretty
clearly showed that most of the time was spent inside the
`Worksheet#num_cols` method so that's where I started optimizing. Just
memoizing the result of that method shows such a drastic improvement in
runtime that I didn't bother doing any further optimizations.

ruby-prof call stack reports:

Before:

![ruby-prof-stack-before](https://f.cloud.github.com/assets/1900876/253302/cbc1b0f4-8bcb-11e2-80cb-2ad1d540cbd1.png)

After:

![ruby-prof-stack-after](https://f.cloud.github.com/assets/1900876/253301/cb987018-8bcb-11e2-9e40-038559bb0b79.png)