CS 314 Assignment 1: Timing and Big O

Due: September 12, 2017, in class.

Files: Stopwatch.java    timing.java    log-log.pdf    semi-log.pdf

  1. Examine and test the factorial functions fact(n) where n is int, Integer, long, float, and double. For how large a value of n does fact(n) correctly and accurately compute the factorial in each case? (We will assume that "accurately" means that all digits of the factorial are correct, assuming that any digits not shown in floating-point output are zero.) Be careful: most students get this wrong. Did you get any indication (such as a runtime exception) if the value of the factorial was incorrect?

  2. The file timing.java contains some mystery functions mickey, minnie, donald, goofy, pluto, gyro, and daffy, and also a BigInteger version of fact. We want to analyze the performance of these functions experimentally.

    Use the Stopwatch to time the functions:

    Stopwatch timer = new Stopwatch();
    
       ... initialize arguments first
    timer.start();
       ... code to be timed
    timer.stop();
    
    System.out.println(timer);
    

    For daffy and donald, time the functions for values of n from 30 through 44 . Plot these times (if appropriate) on semi-log paper.

    For mickey, minnie, goofy, and pluto, initialize by making an array of size n with randomarr(n). The initialization of the array should not be inside the timing calls. Use values of n of 1000, 2000, 4000, 8000 ... through 256000 (for mickey, go up to 8192000).

    For gyro, make an array using randomarr(n) and call pluto on it first. (pluto should not be included in the timing). Use values of n of 1000, 2000, 4000 ... through 256000 .

    For fact(BigInteger n), test on values of n from 1000 through 64000, doubling each time. You can make a BigInteger using code such as:

    BigInteger bign = BigInteger.valueOf((long) 1000);
    
    A BigInteger bign that is not too large can be converted to an int using bign.intValue() . There are operations .add(), .subtract(), .multiply(), and .divide(), each with a BigInteger argument (this is just for your information).

    For each of the functions, plot the time taken vs. n on log-log paper and estimate the big-O for the function. Note that the timer will not be accurate for very small times, e.g. less than a millisecond (0.001 second). If all the times of a function are too small, just report that the function is too fast to measure (and don't plot it). Do not include times that are too small in your plots, since the reported times are just noise.

    For mickey, minnie, goofy, and pluto, also estimate the Big O of each function using the ratio of times as the input size is doubled. The time of the stopwatch in seconds can be obtained (as double) as timer.time(). For small values of the input size, the times will be noisy and the ratio will not be meaningful, so you may need to ignore the first few ratio values and concentrate on the ratios for larger inputs.