On this page, you will use an algorithm timer to help you compare the efficiency of algorithms.
Snap! allows us to report how long a program takes to finish.
current (
date)
reporter. Drag it into the scripting area. From its input menu, select time in milliseconds.
At the bottom of the Variables palette, there will now be a block called time function
. It takes any reporter (with its inputs filled in), computes the result, and reports how long it took to do the computation (in milliseconds).
list from
block with any reporter. You can edit time function
to see how it works.In this example, it took 27 milliseconds to compute the list of integers from 1 to 1000. (The number you see reported will depend on how fast your computer is and what other programs are running on it.)
Here's how time function
works:
time function
to compare Alphie and Betsy's ways of adding the integers from 1 to n. Try it with several different large numbers to see just how different the algorithms are in terms of the time it takes to compute their outputs.A linear search means searching a list from one item to the next.
A binary search means dividing a sorted list into two pieces at each step.
position of () in unsorted list
works for any list by checking element-by-element until it finds a match. This is called a linear search because the program searches from the beginning of the list to the match in a straight line.position of () in sorted list
works on sorted lists by repeatedly dividing the list into two equal-sized parts and using the middle value to decide which half can contain the match. This is called a binary search because binary means "two" and the algorithm divides the list into two parts.Time your algorithms for varying size inputs and describe the different behaviors you see. Again, use large inputs for meaningful results.
time function
should really betime expression
. --MF, 2/20/18