isefire

StopwatchExtended

Jul 27th, 2014
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. public class Stopwatch
  2. {
  3.     private long t;
  4.     private double a;
  5.     private double z;
  6.  
  7.     public Stopwatch()
  8.     {
  9.         t = System.currentTimeMillis();
  10.         a = 0;
  11.         z = 0;
  12.     }
  13.  
  14.     public double elapsedTime()
  15.     {
  16.         return (System.currentTimeMillis() - t) / 1000.0;
  17.     }
  18.     public double[] stop()
  19.     {
  20.         //currentlap
  21.         z = elapsedTime();
  22.         //total
  23.         a += z;
  24.         t = System.currentTimeMillis();
  25.         double[] x = {a, z};
  26.         return x;
  27.  
  28.     }
  29.     public void restart()
  30.     {
  31.         t = System.currentTimeMillis();
  32.     }
  33.     public static void main(String[] args)
  34.     {
  35.         int N = Integer.parseInt(args[0]);
  36.  
  37.         double totalMath = 0.0;
  38.         Stopwatch swMath = new Stopwatch();
  39.         for (int i = 0; i < N; i++)
  40.         {
  41.             totalMath += Math.sqrt(i);
  42.         }
  43.         double timeMath = swMath.elapsedTime();
  44.  
  45.  
  46.  
  47.         double totalNewton = 0.0;
  48.         Stopwatch swNewton = new Stopwatch();
  49.         for (int i= 0; i < N; i++)
  50.         {
  51.             totalNewton += Newton.sqrt(i);
  52.         }
  53.         double timeNewton = swNewton.elapsedTime();
  54.  
  55.         StdOut.println(totalNewton/totalMath);
  56.         StdOut.println(timeNewton/timeMath);
  57.     }
  58. }
Add Comment
Please, Sign In to add comment