I wrote a java program to test the capabilities of processors by running an amount of calculations and then matching it up against my friends computers.
However when I run the program, it doesn’t use 100% of the processor. The processing power goes from 1-2% to 27% and the RAM stays at 34%.
Is this just the way java or processors work? or is it something with my code? Here’s the class that handles the calculation(Note: I’m still learning how to program, and I’m interested in the way the software interacts with the hardware):
import javax.swing.JTextPane;
public class Main {
static int numberToCalculate = 100;
static int otherNumberToCalculate = 50;
static String typeOfCalculation = "all";
static int calculated;
static int calculations = 10000000;
public static void calculate(JTextPane j, JTextPane j2) {
long time = System.currentTimeMillis();
for(int i = 0; i <= calculations; i++) {
switch(typeOfCalculation) {
case "Divide":
calculated = numberToCalculate / otherNumberToCalculate;
break;
case "Multiply":
calculated = numberToCalculate * otherNumberToCalculate;
break;
case "Plus":
calculated = numberToCalculate + otherNumberToCalculate;
break;
case "Minus":
calculated = numberToCalculate - otherNumberToCalculate;
break;
case "All":
calculated = numberToCalculate / otherNumberToCalculate;
calculated = calculated * otherNumberToCalculate;
calculated = calculated + otherNumberToCalculate;
calculated = calculated - otherNumberToCalculate;
break;
default:
Test.UpdateText(j, "Error, please pick type of calculation.");
Test.UpdateText(j2, "Error, please pick type of calculation.");
break;
}
if(i == calculations) {
Test.UpdateText(j, "Milliseconds: " + (System.currentTimeMillis() - time));
Test.UpdateText(j2, "Result: " + calculated);
}
}
}
public static void main(String [] args)
{
Test.window();
}
}
And here’s a picture of the output: https://i.stack.imgur.com/lH1VA.png
If you’re on a multi-processor machine, you’re only going to be maxing out one processor with this code. I’m going to go out on a limb and guess that you have 4 processors (or 2 with hyperthreading). That would explain why you’re only getting 27% utilization.
You’ll need to spin up extra threads doing calculations as well if you want to truly max out all of the cores in your system.