Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

966 CHAPTER 29: Tips and Techniques for Improving Your Scripts original list's items. This is similar to the strategy we used in our binary search: halve the original list, then quarter it, and so on. Once the two smaller lists have been created, line 22 sorts each list recursively. So, for instance, if you start with a 16-item list, this will be split into two smaller lists of roughly 8 items each. Each of those lists then divides into two 4-item lists, and so on. If you need to sort a 32-item list, then that adds one more level of recursion. Let's look at this handler in terms of efficiency. As you can see, it contains two repeat statements, one inside the other. The repeat statement starting on line 12 loops over a fixed-length list, so we just ignore that. The loop on lines 13 to 20 counts over a variable- length list, so that is O(n). On top of that, we have recursive calls happening on line 22. Doubling the number of list items adds roughly one more level of recursion, which is logarithmic, or O(log n), behavior. Since the looping happens inside the recursion, multiplying the two gives us the efficiency of quicksort as a whole: O(n log n)--not as good as O(n) but much better than O(n 2 ). In practice, the efficiency of quicksort tends to be a bit less than this, since you only get O(log n) efficiency on the recursion stage if the two sublists are equally split. If one list is larger than the other, it will take longer to get through. Some quicksort implementations add extra code to try to improve this balancing act, but even a "pure" quicksort design like this one will, on average, provide pretty impressive performance. Studying algorithms can be an educational experience, providing you with greater insight into the design and performance of your own code. In the end, though, perhaps what matters most is that you can put this quicksort handler to good use in your own scripts, taking advantage of all the hard work that other people have done in creating and sharing really useful code--all the way from the humble quicksort routine to mighty productivity suites such as Microsoft Office and Adobe Creative Suite. And that, surely, is one of the best optimizations of all. Summary Getting to be an experienced scripter is a funny thing: the more I work with AppleScript, the slower and more deliberate I become at writing the scripts, but because of that, the systems I create are more solid and well thought out. I used to rush into things by writing scripts, but now I jot down diagrams and flow charts before I even get to the script editor. Furthermore, I can appreciate the time I spend on writing better scripts when I have to refer to some of my older work. It spreads out on a sort of an evolutionary timeline, with older scripts looking a bit childish and "bad habits" slowly disappearing from them the more current they are. The takeaway from this chapter is that although there is rarely One True Way of developing code, and you can go a long way in AppleScript even without learning "proper" programming skills, you may find some problems are far more easily solved if you have a good range of tools in your scripting toolbox to choose from.