This preview shows page 1. Sign up to view the full content.
Unformatted text preview: me spent sorting to become negligible, and the overall run time to around 1.2 seconds. Part (b) of the ﬁgure shows the times for the remaining version on a scale where we can see them better. With improved sorting, we now ﬁnd that list scanning becomes the bottleneck. Thinking that the inefﬁciency is due to the recursive structure of the function, we replaced it by an iterative one, shown as “Iter First.” Surprisingly, the run time increases to around 1.8 seconds. On closer study, we ﬁnd a subtle difference between the two list functions. The recursive version inserted new elements at the end of the list, while the iterative one inserted them at the front. To maximize performance, we want the most frequent words to occur near the beginnings of the lists. That way, the function will quickly locate the common cases. Assuming words are spread uniformly throughout the document, we would expect the ﬁrst occurrence of a frequent one come before that of a less frequent one. By inserting new words at the end, the ﬁrst function tended to order words in descending order of frequency, while the second function tended to do just the opposite. We therefore created a third list scanning fun...
View Full Document
- Spring '10
- The American