Performance

Small Utility Libraries that give time costs associated with program execution paths are powerful

Easy way to verify on hand vs not on hand factors into initial conditions

Initial conditions have a big sway

Redundant costs can be reduced, Performant from start matters

Potential for Conjecture without easy ways to verify

Clear perceptible differences that code diverges to Infinity or that line takes 5 nanoseconds to execute

Free Starter Classes

package util;

public class PerformanceStopWatch {
	private boolean capturing = false;
	private long startTime = 0;
	
	public boolean startStopWatch() {
		if (!capturing) {
			capturing = true;
			startTime = System.currentTimeMillis();
			return true; // Stop Watch Started
		}
		return false; // Need to Stop First
	}
	
	public long stopStopWatch() {
		capturing = false;
		long timeDelta = System.currentTimeMillis()-startTime;
		return timeDelta;
	}
}
-------
package lang.logging;

public class Logger {
	private String spacingElement="";
	private String name; 
	
	public static Logger generateLogger(String loggerName) {
		Logger logger = new Logger();
		logger.name = loggerName;
		return logger;
	}
	
	public void log(String message) {
		System.out.println(name+spacingElement+message);
	}
}

-------
package test.util;

import lang.logging.Logger;
import util.PerformanceStopWatch;

public class PerformanceStopWatchTest {
	private static Logger lg = Logger.generateLogger("PerformanceStopWatchTest");
	
	public static void main(String args[]) {
		PerformanceStopWatch psw = new PerformanceStopWatch();
		lg.log("Test Performance Time to sum 10,000,000 numbers");
		psw.startStopWatch();
		long total=0;
		for (int i=0;i<10000000;i++) {
			total+=i;
		}
		lg.log("0 to 10,000,000 summed is: "+total);
		// https://en.wikipedia.org/wiki/Order_of_magnitude
		long timeElapsedInMilliseconds = psw.stopStopWatch(); // Milli 10^-3, Micro 10^-6
		double seconds = timeElapsedInMilliseconds/1000.0;
		
		lg.log("Time Required to sum 0 to 10,000,000 integers in seconds: "+String.format("%,.5f seconds", seconds));
		lg.log("Time Required to sum 0 to 10,000,000 integers in milliseconds: "+timeElapsedInMilliseconds);
	}
}

PerformanceStopWatchTest Test Performance Time to sum 10,000,000 numbers
PerformanceStopWatchTest 0 to 10,000,000 summed is: 49999995000000
PerformanceStopWatchTest Time Required to sum 0 to 10,000,000 integers in seconds: 0.0070000 seconds
PerformanceStopWatchTest Time Required to sum 0 to 10,000,000 integers in milliseconds: 7

Tested on my Machine MSI GF65 Thin 10UE Microsoft Windows 11 Home

Processor Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz, 2592 Mhz, 6 Core(s), 12 Logical Processor(s)

Bonus class

package util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FileUtil {
	private static String fileDivider = "\\";
	
	public static String getFileDivider() {
		return fileDivider;
	}

	public static String getWorkingDirectory() {
		File file = new File("");
		String workingDirectory = file.getAbsolutePath();
		return workingDirectory;
	}
	
	public static String getFileAsString(String filename) throws FileNotFoundException {
		String fileAsString = "";
		File file = new File(filename);
		Scanner scanner = new Scanner(file);
		while (scanner.hasNextLine()) {
			fileAsString += scanner.nextLine()+"\n"; 
		}
		
		scanner.close();
		
		return fileAsString.substring(0,fileAsString.length()-1);
	}
	
	public static void writeStringAsFile(String filename, String fileAsString) throws IOException {
		File file = new File(filename);
		FileWriter fw = new FileWriter(file);
		fw.write(fileAsString);
		fw.close();
	}
}

Was originally dividing by 1000 instead of 1000.0 and was rounding the seconds value to 0

Would have expected the long value divided by 1000 and set to double to not be rounded by default, might be some reasoning behind that design choice

Original

double seconds = timeElapsedInMilliseconds/1000;

Output (truncated to key details, relevant lines)
------
PerformanceStopWatchTest Time Required to sum 0 to 10,000,000 integers in seconds: 0.0000 seconds
PerformanceStopWatchTest Time Required to sum 0 to 10,000,000 integers in milliseconds: 6

Updated

double seconds = timeElapsedInMilliseconds/1000.0;

Output (truncated to key details, relevant lines)
------
PerformanceStopWatchTest Time Required to sum 0 to 10,000,000 integers in seconds: 0.010000 seconds
PerformanceStopWatchTest Time Required to sum 0 to 10,000,000 integers in milliseconds: 10

https://www.dictionary.com/browse/truncate

On a different note, Engineers and Artists being stuck in corrupt Jails is High Value Out for Societal Performance and Peace

Thus

Published by techinfodebug

Flex and Java Developer, Christian, Art, Music, Video, and Vlogging

Leave a comment