Snap! allows us to report how long a program takes to finish. In the "Sensing" palette, look for the reset timer
command and a reporter called timer
.
timer
on the stage (click the checkbox), and you'll see a timer ticking away in the top left corner of the stage. It’s been ticking ever since you opened Snap!:reset timer
and see what happens.
The
timer
keeps chugging along, but the current
reporter is another useful feature of Snap!. If you set its input to time in milliseconds
, it reports the current time. This can be used to time how long a reporter takes to output. Here’s an algorithm:
list between
block for any reporter—this may be a new idea. Open time function
to see how it works. time function
implements this algorithm.
In this example, it took 27 milliseconds to compute the list of integers from 1 to 1000.
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.position of __ in unsorted list
works for any list as a linear search, checking element-by-element until it finds a match (or gets to the end and reports NIL). The reporter position of __ in sorted list
works on sorted lists as a binary search algorithm.
So, both will work on sorted lists. Compare them for some long sorted lists and make a table of your findings.
Time your algorithms for varying size inputs and describe the different behaviors you see.
average time
that allows you to input a reporter and a number of trials. Its output is the average of the time it takes the reporter to compute its output for the the specified number of trials. Here’s a picture of how it works for Alphie’s summing reporter, adding the numbers from 1 to 500, performed 10 times:average time
reports different times on different calls. What could make it more consistent?