Java 什么';是什么导致我的程序处理时间过长?
我目前正在做一个学校项目,该项目将解析包含不同移动指令的文本文件。然后将这些指令放在一个树状结构中,该树状结构总是向右显示一条新指令,向左显示每条指令的执行量 它看起来像这样:Java 什么';是什么导致我的程序处理时间过长?,java,performance,Java,Performance,我目前正在做一个学校项目,该项目将解析包含不同移动指令的文本文件。然后将这些指令放在一个树状结构中,该树状结构总是向右显示一条新指令,向左显示每条指令的执行量 它看起来像这样: FORW / \ 2 LEFT / \ 90 Rep / \ FORW FORW
FORW
/ \
2 LEFT
/ \
90 Rep
/ \
FORW FORW
/ /
4 2
不管怎么说,在不深入了解我的课程细节的情况下,我想问你的问题是,我如何改进课程的这一部分,使它变得更快
正如你所看到的,我已经尝试使用stringbuilder来加速这个过程,但是没有太大的不同,在这一点上我有点不知所措,所以任何帮助都将不胜感激
这个程序用很多测试用例进行测试,如果其中任何一个测试用例花费的时间超过7秒,它就会失败,现在就是这样
import java.util.ArrayList;
// Ett syntaxträd
public class CreateLines{
private boolean penDown; // 1 om pennan är nere 0 om inte.
private double x1, x2, y1, y2; // x,y-koordinat
private int degrees; // vinkel v
private String color; // nuvarande färg
private double piDegrees;
private double decimals;
private ArrayList<String> result;
public CreateLines() {
penDown = false;
x1 = 0;
x2 = 0;
y1 = 0;
y2 = 0;
degrees = 0;
color = "#0000FF";
// For optimization.
decimals = 100000;
}
public StringBuilder treeSearch (Operation tree) {
// Some variables we use down here:
double x1 = this.x1;
double y1 = this.y1;
double x2;
double y2;
int numberNode;
StringBuilder str = new StringBuilder();
switch (tree.operation) {
case FORW:
piDegrees = Math.PI*degrees/180;
numberNode = tree.evaluate();
x2 = x1 + (numberNode * Math.cos(piDegrees));
y2 = y1 + (numberNode * Math.sin(piDegrees));
x2 = (double)Math.rint(x2 * decimals) / decimals;
y2 = (double)Math.rint(y2 * decimals) / decimals;
this.x1 = x2;
this.x2 = x2;
this.y1 = y2;
this.y2 = y2;
// If penDown then we print otherwise not.
if (penDown){
str.append(color + " " + x1 + " " + y1 + " " + x2 + " " + y2 + "\n");
if(tree.right == null){
return str;
}
return str.append(treeSearch((Operation) tree.right));
}
else{
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
case BACK:
piDegrees = Math.PI*degrees/180;
numberNode = tree.evaluate();
x2 = x1 - (numberNode * Math.cos(piDegrees));
y2 = y1 - (numberNode * Math.sin(piDegrees));
x2 = (double)Math.rint(x2 * decimals) / decimals;
y2 = (double)Math.rint(y2 * decimals) / decimals;
this.x1 = x2;
this.x2 = x2;
this.y1 = y2;
this.y2 = y2;
// If penDown then we print otherwise not.
if (penDown){
str.append(color + " " + x1 + " " + y1 + " " + x2 + " " + y2 + "\n");
if(tree.right == null){
return str;
}
return str.append(treeSearch((Operation) tree.right));
}
else{
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
case LEFT:
numberNode = tree.evaluate();
this.degrees = degrees+numberNode;
if (penDown){
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
else{
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
case RIGHT:
numberNode = tree.evaluate();
this.degrees = degrees-numberNode;
if (penDown){
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
else{
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
case DOWN:
this.penDown = true;
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
case UP:
this.penDown = false;
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
case COLOR:
this.color = tree.color.toUpperCase();
if (penDown){
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
else{
if(tree.right == null){
return str;
}
return treeSearch((Operation) tree.right);
}
case REP:
// if we got a rep instruction to the left we
if(tree.right == null){
for(int i = 0; i < tree.rep; i++){
str.append(treeSearch((Operation) tree.left));
}
return str;
}
else {
for(int i = 0; i < tree.rep; i++){
str.append(treeSearch((Operation) tree.left));
}
return str.append(treeSearch((Operation)tree.right));
}
}
assert false; // borde aldrig kunna hända
return null;
}
}
import java.util.ArrayList;
//埃特综合征
公共类创建行{
私有布尔值penDown;//1 om pennanär nere 0 om inte。
专用双x1,x2,y1,y2;//x,y坐标
私有整数度;//vinkel v
私有字符串颜色;//nuvarande färg
私家双鹭;
私有双小数;
私有数组列表结果;
公共CreateLines(){
penDown=假;
x1=0;
x2=0;
y1=0;
y2=0;
度=0;
color=“#0000FF”;
//用于优化。
小数=100000;
}
公共StringBuilder树搜索(操作树){
//我们在下面使用的一些变量:
双x1=这1.x1;
双y1=这1.y1;
双x2;
双y2;
整数节点;
StringBuilder str=新的StringBuilder();
开关(树操作){
案例:
piDegrees=数学PI*度/180;
numberNode=tree.evaluate();
x2=x1+(numberNode*Math.cos(piDegrees));
y2=y1+(numberNode*Math.sin(piDegrees));
x2=(双精度)数学打印(x2*小数)/小数;
y2=(双精度)数学打印(y2*小数)/小数;
这1.x1=x2;
这是0.x2=x2;
这是1.y1=y2;
这1.y2=y2;
//如果是penDown,则我们打印,否则不打印。
如果(彭敦){
str.append(颜色+“”+x1+“”+y1+“”+x2+“”+y2+“\n”);
if(tree.right==null){
返回str;
}
返回str.append(treeSearch((Operation)tree.right));
}
否则{
if(tree.right==null){
返回str;
}
返回树搜索((操作)tree.right);
}
案件背景:
piDegrees=数学PI*度/180;
numberNode=tree.evaluate();
x2=x1-(numberNode*Math.cos(piDegrees));
y2=y1-(numberNode*Math.sin(piDegrees));
x2=(双精度)数学打印(x2*小数)/小数;
y2=(双精度)数学打印(y2*小数)/小数;
这1.x1=x2;
这是0.x2=x2;
这是1.y1=y2;
这1.y2=y2;
//如果是penDown,则我们打印,否则不打印。
如果(彭敦){
str.append(颜色+“”+x1+“”+y1+“”+x2+“”+y2+“\n”);
if(tree.right==null){
返回str;
}
返回str.append(treeSearch((Operation)tree.right));
}
否则{
if(tree.right==null){
返回str;
}
返回树搜索((操作)tree.right);
}
案例左:
numberNode=tree.evaluate();
this.degrees=度+数节点;
如果(彭敦){
if(tree.right==null){
返回str;
}
返回树搜索((操作)tree.right);
}
否则{
if(tree.right==null){
返回str;
}
返回树搜索((操作)tree.right);
}
案例权利:
numberNode=tree.evaluate();
this.degrees=度数节点;
如果(彭敦){
if(tree.right==null){
返回str;
}
返回树搜索((操作)tree.right);
}
否则{
if(tree.right==null){
返回str;
}
返回树搜索((操作)tree.right);
}
按大小写:
this.penDown=true;
如果(树)