我编写了一个 java 程序,通过运行大量计算然后将其与我朋友的计算机进行匹配来测试处理器的功能。
但是,当我运行该程序时,它并没有使用 100% 的处理器。处理能力从 1-2% 下降到 27%,RAM 保持在 34%。
这只是java或处理器的工作方式吗?还是我的代码有问题?这是处理计算的类(注意:我还在学习如何编程,我对软件与硬件交互的方式很感兴趣):
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();
}
}
这是输出的图片:http: //i.stack.imgur.com/lH1VA.png