# Homework 12

## Background

In this homework, I applied the ruby profiler to two programs. The first program, fastrubysearch.rb, is a variant of the search program that was used in Lab 5. It creates three large arrays, searches them using a binary search algorithm and prints output that shows how long it took to create the arrays and how long it took to search them. The output produced by this program is located in fast.out and the output produced by the profiler for this program is located in fast.html. The second program, slowrubysearch.rb, is a variant of the first program in that it uses a linear search algorithm rather than a binary search to search the arrays. The output produced by this program is located in slow.out and the output produced by the profiler for this program is located in slow.html.

As you can see, linear search took a lot longer to search the arrays, spending 382 seconds versus 0.32 seconds for binary search!!

For an explanation of the profiler's output, take a look at this call graph tutorial that is located on the ruby profiler website.

You will need to understand the call graphs produced by the profiler to answer the questions below.

1. How long did the fastrubysearch.rb program spend searching the arrays? What percentage is that out of its total running time? (2 points)
2. How long did the slowrubysearch.rb program spend searching the arrays? What percentage is that out of its total running time? (2 points)
3. Why did the fastrubysearch.rb program spend so much less time searching the arrays than slowrubysearch.rb? (2 points)
4. Despite taking a long time to search the arrays, how much time was actually spent in the search() method in slowrubysearch.rb? (1 point)
5. One of the functions called by the search() method in slowrubysearch.rb is Fixnum#==. What percentage of time over the entire program run time did the search() method spend comparing the values in the arrays to its target? (1 point)
6. According to the profiler, the fastrubysearch.rb didn't spend any noticeable amount of time comparing values in the array to the search target? Why is that, and what Fixnum operator was used more by the binsearch() method? (2 points)
7. The Array#[] method is used to access a specific element of an array. Describe the differences in how this method was used by the two programs using information from the two profiler reports. (2 points)