Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.  *  Copyright Christopher Lemire
  3.  *  Version 0.000000000001 (You need floating point precision for this version)
  4.  *
  5.  *  This program is free software: you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation, either version 3 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18. package test.ch.expectusafterlu;
  19.  
  20. import static java.lang.System.*;
  21. import java.util.Arrays;
  22.  
  23. public class ApproximateAccurateTime {
  24.  
  25.   public static void main(String weechat[]) {
  26.  
  27.     out.print("This project was created by Christopher Lemire"
  28.             + "\n\nDo NOT attempt to do this at home! "
  29.             + "\nOr your computer may catch on fire :P"
  30.             + "\nThis demo illustrates comparing times in nanoseconds.");
  31.  
  32.     out.println(); // Print an extra \n new line
  33.  
  34.     int count = 0; // Using ANSI c89 Syntax
  35.     for (count = 0; count < 2; ++count) {
  36.  
  37.       // Declare and Initialize without random values, using 0/null
  38.       // It's more explicit but not necessarily needed
  39.       long startNanoT = 0;
  40.       long finishNanoT = 0;
  41.  
  42.       // Initialize startNanoT with current time in nanoseconds
  43.       startNanoT = System.nanoTime();
  44.  
  45.       out.printf("\n*** Starting Test # %d ***\n\n", (count + 1));
  46.  
  47.       // Calculate disk space, comparing times before and after in nanoseconds
  48.       if (count == 0) { // First pass
  49.         simpleCompute();
  50.       } else if (count == 1) { // Second pass
  51.         bubbleSort();
  52.       } else {
  53.         err.print("\tMake a bug report :P");
  54.         exit(-1);
  55.       }
  56.  
  57.       // Initialize finishNanoT with current time in nanoseconds
  58.       finishNanoT = System.nanoTime();
  59.  
  60.       long diffNanoT = finishNanoT - startNanoT; // Difference in nanoseconds
  61.       double secondsT = (double) diffNanoT / 1000000000.0;
  62.  
  63.       out.printf("Total time to complete in Nanoseconds:\t%g\n", secondsT);
  64.  
  65. //TODO Simple Math out.printf("Seconds: %d, MilliSeconds %d, NanoSeconeds: %d", "")
  66. //TODO "Current time in Nanoseconds: %d\n\n", (count + 1), finishNanoT)"
  67.       out.printf("Total time to complete in Seconds:\t%f\n\n", secondsT);
  68.       out.printf("*** Finished Test # %d ***\n", (count + 1));
  69.     }
  70.   }
  71.  
  72.   protected static void simpleCompute() {
  73.     /* Computes total disk space for
  74.      * 2x320gb drives
  75.      * 1.5 tb drive and
  76.      * 1 tb drive
  77.      */
  78.     int gbTotal = 320 * 2 + (int) (1024 * 1.5) + 1024;
  79.     out.println("\tTotal disk space:\n"
  80.             + "\t" + gbTotal / 1024 + " TB " + gbTotal % 1024
  81.             + " GB");
  82.     out.println();
  83.   }
  84.  
  85.   /*
  86.    * TODO Use more OOP instead of static methods
  87.    * Computationally expensive, add more numbers
  88.    */
  89.   protected static void bubbleSort() {
  90. //    int[] a = {3, 6, 2, 4, 20, 10, 5, 3, 8};
  91.     int[] a = {3, 6, 2, 4, 20, 10, 5, 3, 8, 200, 150, 333, 0, 47, 500};
  92.  
  93.     out.printf("\t" + Arrays.toString(a).concat("\n"));
  94.  
  95.     int temp;
  96.     for (int pass = 1; pass < a.length; pass++) {
  97.       for (int i = 0; i < a.length - 1; i++) {
  98.         if (a[i] > a[i + 1]) {
  99.           temp = a[i];
  100.           a[i] = a[i + 1];
  101.           a[i + 1] = temp;
  102.         }
  103.       }
  104.     }
  105.  
  106.     out.print("\t" + Arrays.toString(a) + "\n");
  107.     out.println();
  108.  
  109.   }
  110. }