How can you make your program run faster?

edited November 2013 in How To...

It seems that many of my "big" programs run slower than I wish they did. For example, when I use two for loops to loop through about 100 things. How can I make them faster? Is there some way to give the program more memory?

Tagged:

Answers

  • This online example uses double loop. Check whether it works fast for you:
    http://studio.processingtogether.com/sp/pad/export/ro.9s0026dE7B8v-/latest

  • You should post some code, increasing the memory is probably not the problem

  • I'd say first find out what is slow. I have a very small timer system where i have 5 generic timers that i can start and stop around a chunk or single line of code. Then i print that out to the screen or println. Sometimes it's obvious what's slow and other times not.

  • edited November 2013

    This is the code that causes the lag, when I remove it its fine. And by the way, it is OK in java mode, but very slow in android mode which is where is want to use this code.

        int BALL_NUM = 100;
        void setup() {
    //code
    }
    void draw() {
    //code
        for(int i=0; i<BALL_NUM; i++)
        for(int j=1; j<BALL_NUM; j++) {
        stroke(0);
        strokeWeight(1);
        line(ball[i].x,ball[i].y,ball[j].x,ball[j].y);
        }
    }
    class Ball {
    //code
    }
    
  • Answer ✓

    you can move stroke() and strokeweight() to before the outer loop for one thing.

    you can change the second loop to

    for (j = i + 1 ; j < BALL_NUM; j++) {

    (you don't need to draw a line from b to a if you've already drawn one from a to b)

  • Thanks koogs!

  • edited December 2017

    I'm having a problem with my sketch running slow also. I'm making a 3D game with a lot of different objects in the game like enemies, obstacles and things like that. The issue is, once I start adding more and more things to the game, it runs really, really slow. Is there a way I can make it run faster. Here's my code:

    import peasy.*;
    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    int button1 = 1;
    int button2 = 1;
    int button3 = 1;
    int button4 = 1;
    int linPot = 0;
    int liteSensor, tempSensor, mic;
    int jX, jY;
    int aX, aY, aZ;
    int jButton = 1;
    
    boolean test = false; // new
    
    PeasyCam cam;
    int x=75,y=0,z=175;
    int xMov = 0;
    int yMov = 0;
    int zMov = 0;
    Tent tent1;
    Tent tent2;
    Tent tent3;
    Tent tent4;
    Tent tent5;
    Tent tent6;
    Tent tent7;
    Tent tent8;
    Tent tent9;
    Tent tent10;
    Tent tent11;
    Tent tent12;
    Tent tent13;
    Tent tent14;
    Tent tent15;
    Tent tent16;
    Tent tent17;
    ArrayList<Tree> aForest;
    int cY = 0, cZ = 0;
    float ccy=0,ccz=0;
    float xAngle = 0;
    void setup(){
     //fullScreen(P3D);
     size(683,384,P3D);
     frameRate(120);
     if (test) {
        frameRate(2); // slow everything down
      } else {
        String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
        myPort = new Serial(this, portName, 300);
      }
    
    
     //cam.setSuppressRollRotationMode();
    
    aForest = new ArrayList<Tree>();        /*
    for (int z = -100; z < 300; z+=10){
       for (int x = 175; x < 195; x+=10){ 
         Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
         aForest.add(tree);
       }
     }
     for (int z = 55; z < 75; z+=10){
       for (int x = -20; x < 130; x+=10){
         Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
         aForest.add(tree);
       }
     }                                                                    //THIS AREA
     for (int z = -100; z < -50; z+=10){
       for (int x = -20; x < 175; x+=10){
         Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
         aForest.add(tree);
       }
     }
     for (int z = -100; z < 75; z+=10){
       for (int x = -50; x < -20; x+=10){
         Tree tree = new Tree(random(5,10),random(2,5),random(5,15),#1e932d,#8B4513,x,0,z);
         aForest.add(tree);
       }
     }        */
    
    
    tent1 = new Tent(#f4a460,130,0,100,0);
    tent2 = new Tent(#f4a460,100,0,100,0);
    tent3 = new Tent(#f4a460,70,0,100,0);
    tent4 = new Tent(#f4a460,40,0,100,0);
    tent5 = new Tent(#f4a460,10,0,100,0);
    tent6 = new Tent(#f4a460,130,0,140,0);
    tent7 = new Tent(#f4a460,100,0,140,0);
    tent8 = new Tent(#f4a460,70,0,140,0);
    tent9 = new Tent(#f4a460,40,0,140,0);
    tent10 = new Tent(#f4a460,10,0,140,0);
    tent11 = new Tent(#f4a460,150,0,200,1);
    tent12 = new Tent(#f4a460,150,0,230,1);
    tent13 = new Tent(#f4a460,90,0,240,0);
    tent14 = new Tent(#f4a460,60,0,240,0);
    tent15 = new Tent(#f4a460,120,0,300,0);
    tent16 = new Tent(#f4a460,90,0,300,0);
    tent17 = new Tent(#f4a460,60,0,300,0);
    }
    void draw(){
     background(105); 
      println(frameRate);
     cc();
     //println(cam.getPosition());
     //directionalLight(255,255,255,0,1,0);
    
     translate(width/2,height+1,0);
     fill(0,255,0);                       //(height/2)/tan(PI/6)
     stroke(0);                        //translate(x,y,z);
     box(width,-1,width);            //box(width,height,depth)
     fill(255,0,0);
    
     tent1.display();
     tent2.display();
     tent3.display();
     tent4.display();
     tent5.display();
     tent6.display();
     tent7.display();
     tent8.display();
     tent9.display();
     tent10.display();
     tent11.display();
     tent12.display();
     tent13.display();
     tent14.display();
     tent15.display();
     tent16.display();
     tent17.display();
    
     for(Tree singleTree: aForest){  //Check: <a href="https://processing.org/reference/for.html" target="_blank" rel="nofollow">https://processing.org/reference/for.html</a>;
       singleTree.display();
    }
    
    
     translate(x,y-2.5,z);
    // rotateY(turn);
     fill(0);
     stroke(255,0,0);
     sphere(5);
    
     x += xMov;
     y += yMov;
     z += zMov;
     println(x,",",y,",",z);
    }
    
    class Tent{
      color c;
      float tX;
      float tY;
      float tZ;
      float d;
     Tent(color tempC, float tempX, float tempY, float tempZ, float tempD){
       d = tempD;
       c = tempC;
       tX = tempX;
       tY = tempY;
       tZ = tempZ;
       }
       void display(){
         if(d == 0){
         stroke(0);
         fill(c);
         beginShape();
         vertex(tX-10,tY,tZ-10); //left rear
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX+10,tY,tZ-10); //right rear
         endShape(CLOSE);
         beginShape();
         vertex(tX,tY-10,tZ-10); //top rear
         vertex(tX,tY-10,tZ+10); //top front
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX-10,tY,tZ-10); //left rear
         endShape(CLOSE);
         beginShape();
         vertex(tX,tY-10,tZ-10); //top rear
         vertex(tX,tY-10,tZ+10); //top front
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX+10,tY,tZ-10); //right rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY,tZ-10); //left rear
         vertex(tX+10,tY,tZ-10); //right rear
         vertex(tX,tY-10,tZ-10); //top rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX,tY-10,tZ+10); //top front
         endShape(CLOSE);
         }else if(d == 1){
         stroke(0);
         fill(c);
         beginShape();
         vertex(tX-10,tY,tZ-10); //left rear
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX+10,tY,tZ-10); //right rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY-10,tZ); //top left
         vertex(tX+10,tY-10,tZ); //top right
         vertex(tX+10,tY,tZ-10); //right rear
         vertex(tX-10,tY,tZ-10); //left rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY-10,tZ); //top left
         vertex(tX+10,tY-10,tZ); //top right
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX-10,tY,tZ+10); //left front
         endShape(CLOSE);
         beginShape();
         vertex(tX+10,tY,tZ+10); //right front
         vertex(tX+10,tY,tZ-10); //right rear
         vertex(tX+10,tY-10,tZ); //top right
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY,tZ+10); //left front
         vertex(tX-10,tY,tZ-10); //left rear
         vertex(tX-10,tY-10,tZ); //top left
         endShape(CLOSE);
         }else if(d == 2){
         stroke(0);
         fill(c);
         beginShape();
         vertex(tX-10,tY,tZ); //right
         vertex(tX,tY,tZ+10); //front
         vertex(tX+10,tY,tZ); //left
         vertex(tX,tY,tZ-10); //rear
         endShape(CLOSE);
         beginShape();
         vertex(tX-10,tY,tZ);     //right
         vertex(tX,tY,tZ-10);     //rear
         vertex(tX-5,tY-10,tZ-5); //right rear top
         endShape(CLOSE);
         beginShape();
         vertex(tX,tY,tZ+10);     //front
         vertex(tX+10,tY,tZ);     //left
         vertex(tX+5,tY-10,tZ+5); //left front top
         endShape(CLOSE); 
         beginShape();
         vertex(tX-10,tY,tZ); //right
         vertex(tX,tY,tZ+10); //front
         vertex(tX+5,tY-10,tZ+5); //left front top
         vertex(tX-5,tY-10,tZ-5); //right rear top
         endShape(CLOSE);
         beginShape();
         vertex(tX+10,tY,tZ); //left
         vertex(tX,tY,tZ-10); //rear
         vertex(tX-5,tY-10,tZ-5); //right rear top
         vertex(tX+5,tY-10,tZ+5); //left front top
         endShape(CLOSE);
         }
     }
    }
    
    class Tree {
     float h;
     float tS;
     float lS;
     color lC;
     color tC;
     float treeX,treeY,treeZ;
       Tree (float trunkHeight, float trunkSize, float leafSize, color leafColor, color trunkColor, int treex, int treey, int treez){
         h = trunkHeight;
         tS = trunkSize;
         lS = leafSize;
         lC = leafColor;
         tC = trunkColor;
         treeX = treex;
         treeY = treey;
         treeZ = treez;
       }
       void display(){         
       fill(tC);
       beginShape();
       vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ+(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ+(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ+(tS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX+(tS/2),treeY,treeZ-(tS/2));
       vertex(treeX+(tS/2),treeY-h,treeZ-(tS/2));
       vertex(treeX-(tS/2),treeY-h,treeZ-(tS/2));
       endShape(CLOSE);
       fill(lC);
       beginShape();
       vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ+(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ+(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ+(lS/2));
       endShape(CLOSE);
       beginShape();
       vertex(treeX-(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX+(lS/2),treeY-h,treeZ-(lS/2));
       vertex(treeX+(lS/2),treeY-h-lS,treeZ-(lS/2));
       vertex(treeX-(lS/2),treeY-h-lS,treeZ-(lS/2));
       endShape(CLOSE);
       }
    }
    void keyPressed() {
      if ((key == 'w')) { 
        yMov = -1;
      } else if ((key == 'a')) {  
        xMov = -1;                                   //WASD CONTROLS
      } else if ((key == 'd')) {
        xMov = 1;
      } else if ((key == 's')) {
        yMov = 1;
      } else if((key == 'e')){
       zMov = -1; 
      }else if((key == 'c')){
       zMov = 1; 
      }
    }  
    void keyReleased() {
      if ((key == 'w') || (key == 's')) {
        yMov = 0;                                           //WASD CONTROLS
      } else if ((key == 'a') || (key == 'd')) {
        xMov = 0;
      }else if((key == 'e' ) || (key == 'c')){
       zMov = 0; 
      }
    }
    void cc() {
    
      String val = getData();
      if (val != null) {
        parseData(val);
      }
    
      if (jX >= 15) {
        x-=1;
      } else if (jX <= -15) {
        x+=1;
      }
      if (jY >= 15) {
        z += 1;
      } else if (jY <= -15) {
        z -= 1;
      }
    
    
      cY = 21;
      cam = new PeasyCam(this, (width/2)+x, (height-50)+y, z+15, 50);
      // println(cX,cY,cZ);
      ccy = map(cY, -100, 100, -3, 3);
      cam.setRotations(ccy, 0, 0);
      // println(cX,cY,cZ);
    }
    void parseData(String val) {
      String[] data = split(val, ",");
      if (data == null || data.length != 14) {
      } else {
        button1 = int(data[0]);
        button2 = int(data[1]);
        button3 = int(data[2]);
        button4 = int(data[3]);
        linPot = int(data[4]);
        liteSensor = int(data[5]);
        tempSensor = int(data[6]);
        mic = int(data[7]);
        jButton = int(data[8]);
        jX = int(data[9]);
        jY = int(data[10]);
        aX = int(data[11]);
        aY = int(data[12]);
        aZ = int(data[13]);
      }
    }
    String getData() {
      if (test) {
        return "1,1,1,1,702,1010,20,1,1023,3,-10,12,7,135";
      } else if ( myPort.available() > 0) {
        val = myPort.readStringUntil('\n');
        return val;
      } else {
        return null;
      }
    }
    

    As you can see, there is a lot of code here and i just started, if I note out the area shown, it runs fine, as is, it runs faster, but then its missing a large chunk of the game. There is still more that needs to be added so if there is a way I can get it to run faster, that would be greatly appreciated.

Sign In or Register to comment.