/*
* Copyright Christopher Lemire
* Version 0.000000000001 (You need floating point precision for this version)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package test.ch.expectusafterlu;
import static java.lang.System.*;
import java.util.Arrays;
public class ApproximateAccurateTime {
public static void main(String weechat[]) {
out.print("This project was created by Christopher Lemire"
+ "\n\nDo NOT attempt to do this at home! "
+ "\nOr your computer may catch on fire :P"
+ "\nThis demo illustrates comparing times in nanoseconds.");
out.println(); // Print an extra \n new line
int count = 0; // Using ANSI c89 Syntax
for (count = 0; count < 2; ++count) {
// Declare and Initialize without random values, using 0/null
// It's more explicit but not necessarily needed
long startNanoT = 0;
long finishNanoT = 0;
// Initialize startNanoT with current time in nanoseconds
startNanoT = System.nanoTime();
out.printf("\n*** Starting Test # %d ***\n\n", (count + 1));
// Calculate disk space, comparing times before and after in nanoseconds
if (count == 0) { // First pass
simpleCompute();
} else if (count == 1) { // Second pass
bubbleSort();
} else {
err.print("\tMake a bug report :P");
exit(-1);
}
// Initialize finishNanoT with current time in nanoseconds
finishNanoT = System.nanoTime();
long diffNanoT = finishNanoT - startNanoT; // Difference in nanoseconds
double secondsT = (double) diffNanoT / 1000000000.0;
out.printf("Total time to complete in Nanoseconds:\t%g\n", secondsT);
//TODO Simple Math out.printf("Seconds: %d, MilliSeconds %d, NanoSeconeds: %d", "")
//TODO "Current time in Nanoseconds: %d\n\n", (count + 1), finishNanoT)"
out.printf("Total time to complete in Seconds:\t%f\n\n", secondsT);
out.printf("*** Finished Test # %d ***\n", (count + 1));
}
}
protected static void simpleCompute() {
/* Computes total disk space for
* 2x320gb drives
* 1.5 tb drive and
* 1 tb drive
*/
int gbTotal = 320 * 2 + (int) (1024 * 1.5) + 1024;
out.println("\tTotal disk space:\n"
+ "\t" + gbTotal / 1024 + " TB " + gbTotal % 1024
+ " GB");
out.println();
}
/*
* TODO Use more OOP instead of static methods
* Computationally expensive, add more numbers
*/
protected static void bubbleSort() {
// int[] a = {3, 6, 2, 4, 20, 10, 5, 3, 8};
int[] a = {3, 6, 2, 4, 20, 10, 5, 3, 8, 200, 150, 333, 0, 47, 500};
out.printf("\t" + Arrays.toString(a).concat("\n"));
int temp;
for (int pass = 1; pass < a.length; pass++) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
out.print("\t" + Arrays.toString(a) + "\n");
out.println();
}
}