package stdlib;

/**
 *  <i>Stopwatch</i>. This class is a data type for measuring
 *  the running time (wall clock) of a program.
 *  <p>
 *  For additional documentation, see
 *  <a href="http://introcs.cs.princeton.edu/32class">Section 3.2</a> of
 *  <i>Introduction to Programming in Java: An Interdisciplinary Approach</i>
 *  by Robert Sedgewick and Kevin Wayne.
 */
public class Stopwatch{ 

    private final long start;

   /**
     * Create a stopwatch object.
     */
    public Stopwatch() {
        start = System.currentTimeMillis();
    } 


   /**
     * Return elapsed time (in seconds) since this object was created.
     */
    public double elapsedTime() {
        long now = System.currentTimeMillis();
        return (now - start) / 1000.0;
    }
    
    public static void main(String[] args) { 
        int N = Integer.parseInt(args[0]); 

        // time Math library implementation
        double totalMath = 0.0; 
        Stopwatch swMath = new Stopwatch(); 
        for (int i = 0; i < N; i++) {
            totalMath += Math.sqrt(i); 
        }
        double timeMath = swMath.elapsedTime(); 
  
        // time Newton's method implementation
        double totalNewton = 0.0; 
        Stopwatch swNewton = new Stopwatch(); 
        for (int i = 0; i < N; i++) {
//            totalNewton += Newton.sqrt(i); // Math.sqrt() is about 20 times faster than our Newton.sqrt() (as it should be!). 
        }


        // print results
        double timeNewton = swNewton.elapsedTime(); 
        StdOut.println(totalNewton/totalMath); 
        StdOut.println(timeNewton/timeMath); 
    }
} 
