Free Starter Java Class (MIT Open Source License)
package nsd;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger;
import file.FileUtil;
import performance.PerformanceUtil;
public class NeverSayDieHelloWorld {
private static Logger logger = Logger.getLogger(NeverSayDieHelloWorld.class.getName());
private static void generatePythonScript(String filename) {
final StringWriter stringWriter = new StringWriter();
stringWriter.append("for i in range(100000):\n");
stringWriter.append(" print('Never Say Die!')\n");
stringWriter.append("exit");
File file = new File(filename);
try {
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(stringWriter.toString());
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void runPythonScript(String filename) {
String pythonExecutablePath = "C:\\Windows\\py.exe";
String pythonScriptPath = FileUtil.getWorkingPath()+"\\"+filename;
try {
String commands[] = {
pythonExecutablePath,
pythonScriptPath };
ProcessBuilder processBuilder = new ProcessBuilder(Arrays.asList(commands));
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
PerformanceUtil.getInstance().start();
Process process = processBuilder.start();
CompletableFuture processComplete = process.onExit();
int i=0;
while (!processComplete.isDone() || i>20) {
try {
i++;
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (processComplete.isDone()) {
PerformanceUtil.getInstance().stop();
long timeInNanoseconds = PerformanceUtil.getInstance().getRecordedTime();
logger.info("Time to Run Python Script using ProcessBuilder Class in Java:"+timeInNanoseconds+" nanoseconds");
} else {
logger.info("Process timed out");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String filename = "neverSayDie.py";
generatePythonScript(filename);
runPythonScript(filename);
}
}
Relies on this class, which I also provide Free under MIT Open Source License
package performance;
public class PerformanceUtil {
private boolean stopWatchStarted = false;
private long startTime;
private long endTime;
private long recordedTime=0;
private static PerformanceUtil instance;
public static PerformanceUtil getInstance() {
if (null == instance) {
instance = new PerformanceUtil();
}
return instance;
}
public boolean start() {
if (stopWatchStarted) {
return false;
} else {
startTime=System.nanoTime();
stopWatchStarted=true;
return true;
}
}
public boolean stop() {
if (!stopWatchStarted) {
return false;
} else {
endTime=System.nanoTime();
stopWatchStarted=false;
return true;
}
}
public long getRunningTime() {
long runningTime = System.nanoTime()-startTime;
return runningTime;
}
public long getRecordedTime() {
recordedTime=endTime-startTime;
return recordedTime;
}
}
Outputs the following on my MSI Laptop for Performance, in Eclipse IDE console
....
Never Say Die!
Never Say Die!
Never Say Die!
Never Say Die!
Never Say Die!
Never Say Die!
Mar 10, 2023 12:06:33 PM nsd.NeverSayDieHelloWorld runPythonScript
INFO: Time to Run Python Script using ProcessBuilder Class in Java:199078799 nanoseconds
Processor – Processor Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz, 2592 Mhz, 6 Core(s), 12 Logical Processor(s)
Logging 100,000 of the same line in python requires 200 ms (199078799 nanoseconds) (I believe this is using single threaded model unless python is doing something behind the scenes I don’t know about)
JNI likely much faster, currently being limited from getting my C++ environment setup
Updated to a Util Class, again Free Starter Class (MIT License)
package file;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger;
import performance.PerformanceUtil;
public class ScriptUtil {
private static Logger logger = Logger.getLogger(ScriptUtil.class.getName());
public static void writePythonScript(String filename, String script) {
File file = new File(filename);
try {
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(script);
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void generatePythonScript(String filename) {
final StringWriter stringWriter = new StringWriter();
stringWriter.append("for i in range(100000):\n");
stringWriter.append(" print('Never Say Die!')\n");
stringWriter.append("exit");
writePythonScript(filename, stringWriter.toString());
}
public static void runPythonScript(String filename) {
String pythonExecutablePath = "C:\\Windows\\py.exe";
String pythonScriptPath = FileUtil.getWorkingPath()+"\\"+filename;
try {
String commands[] = {
pythonExecutablePath,
pythonScriptPath };
ProcessBuilder processBuilder = new ProcessBuilder(Arrays.asList(commands));
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
PerformanceUtil.getInstance().start();
Process process = processBuilder.start();
CompletableFuture<Process> processComplete = process.onExit();
int i=0;
while (!processComplete.isDone() || i>20) {
try {
i++;
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (processComplete.isDone()) {
PerformanceUtil.getInstance().stop();
long timeInNanoseconds = PerformanceUtil.getInstance().getRecordedTime();
logger.info("Time to Run Python Script using ProcessBuilder Class in Java:"+timeInNanoseconds+" nanoseconds");
} else {
logger.info("Process timed out");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Pretty Epic Keyboard on the following, Performant and Attractive Computers for Java Development factors into Encouragement
Epic Keyboard on my old GS63VR Computer Inspired me to look at it
https://us.msi.com/Laptop/Raider-GE78-HX-13VX
Not All Computers are Works of Art
How much Reliability and Fault Tolerance can be depended upon in Client machines is somewhat questionable
What do I get for a Xeon on a Laptop?
Xeon Workstation with Alienware Monitor might have potential
https://en.wikipedia.org/wiki/Blockchain
Oppression set to a digital ledger? Amps for Forgiveness vs Amps for Justice?
Max Profit, people to be trusted with trust worthy ledgers?
Quantum Computing that can compute much much faster equals some have the ability to generate more money on the fly more swiftly?
Appearance of more secure, systems sending ledger entries can’t be rerouted non ideally? Distributed ledgers where sets of people are virtualized onto different networks? Applications made to think they are sending data when they are not, to hear and receive updates to ledger when they are not?
Blockchain and Minority vs Majority Rule for who gets to claim right, correctness of records? Seems problematic
Kind of like naming Remove Freedom install Surveillance as Patriot Act
Thoughts of Freedom Fries
70% agree on the value? while 30% don’t? Blockchain for elections?
People with a stake in Bitcoins
People with a stake in Currency
People with a stake in Previous Contributions like Art