My code is running extremely slow since I implemented a class. (irishrail)

edited April 2018 in Questions about Code

I wanted to make a class for an array of arcs, and somehow this seems to be draining CPU. I've gone through it again and again deleting bits of code and running. I can't figure out which part is causing this but I'm pretty sure it's the 'class Arc' on line 53.

Any tips would be much appreciated:)

String urlStationInfo = "http://" + "api.irishrail.ie/realtime/realtime.asmx/getStationsFilterXML?StationText=";

Arc[] arcs;

XML xmlA; 
XML xmlS; 

XML[] childrenA;
XML[] childrenS;
IntList arrayDueTimes;
IntList arrayLateTimes;
StringList arrayID;
StringList stationID;

int angle=0; 
int scaler = 10;

Timer timer = new Timer(10000);


//________________________________________________________________________________________________________________________________ 
void setup() {
  size(800, 800, P2D);  
  requestStationData();
  requestArrayData();
  arcs = new Arc[arrayDueTimes.size()];

  for (int i = 0; i < arrayDueTimes.size(); i++) {
    arcs[i] = new Arc(360/arrayDueTimes.size(), i);
  }
  timer.start();
} 


//________________________________________________________________________________________________________________________________ 
void draw() {
  background(255,  193,  37);

  if (timer.isFinished()) {   // Every one second, make a new request.
    println("Timer is finished: Requesting values and restarting"); 
    requestStationData();
    requestArrayData();   
    timer.start();  // And restart the timer. 
  }

  for (Arc a : arcs) {
    a.display();
  }
}

//________________________________________________________________________________________________________________________________ 
class Arc {
  float radian;
  int noOfArcs;

  Arc(float tempRadian, int tempNoOfArcs) {
    radian = tempRadian;
    noOfArcs = tempNoOfArcs;
  }

  void display() {   
    float startAngle = 0;  
    float lastAngle = 0;  
    for (int i=0; i<arrayDueTimes.size(); i++) {

      fill(255,0,0);
      stroke(0);
      strokeWeight(0);
      arc(width/2, height/2, scaler*abs(arrayDueTimes.get(i))+arrayLateTimes.get(i), scaler*abs(arrayDueTimes.get(i))+arrayLateTimes.get(i), lastAngle, lastAngle+radians(radian));

      fill(0+i*1);
      stroke(0);
      strokeWeight(0);
      arc(width/2, height/2, scaler*abs(arrayDueTimes.get(i)), scaler*abs(arrayDueTimes.get(i)), lastAngle, lastAngle+radians(radian));
      lastAngle += radians(radian);
      startAngle = startAngle + lastAngle;
    }
  }
}  


//________________________________________________________________________________________________________________________________
class Timer {
  int savedTime;
  boolean running = false;
  int totalTime;

  Timer(int tempTotalTime) {
    totalTime = tempTotalTime;
  }

  void start() {
    running = true;
    savedTime = millis();
  }

  boolean isFinished() {
    int passedTime = millis() - savedTime;
    if (running && passedTime > totalTime) {
      running = false;
      return true;
    } else {
      return false;
    }
  }
}


//________________________________________________________________________________________________________________________________ 
void requestStationData() {   
  xmlS = loadXML(urlStationInfo);  
  childrenS = xmlS.getChildren("objStationFilter");   
  stationID = new StringList();

  for (int i = 0; i < childrenS.length; i++) {
    XML sIDElement = childrenS[i].getChild("StationCode"); 
    String sID = sIDElement.getContent();  
    stationID.append(sID);   
  }   
} 


//________________________________________________________________________________________________________________________________ 
void requestArrayData() {   
  arrayID = new StringList();
  arrayDueTimes = new IntList();
  arrayLateTimes = new IntList();

  //xmlA = loadXML("stationData.xml");
  for (int i = 0; i < childrenS.length; i++) {    
    String myString = stationID.get(i);
    xmlA = loadXML("http://"+"api.irishrail.ie/realtime/realtime.asmx/getStationDataByCodeXML?StationCode=" + myString);  

    childrenA = xmlA.getChildren("objStationData");  

    for (int j = 0; j < childrenA.length; j++) {   
      //println(childrenA.length);
      XML originElement = childrenA[j].getChild("Origin");
      String origin = originElement.getContent();      

      XML destinationElement = childrenA[j].getChild("Destination");
      String destination = destinationElement.getContent();   

      XML dueElement = childrenA[j].getChild("Duein");
      int due = dueElement.getIntContent();     
      arrayDueTimes.append(due);

      XML fullNameElement= childrenA[j].getChild("Stationfullname");
      String fullName = fullNameElement.getContent();   
      arrayID.append(fullName);

      XML lateElement = childrenA[j].getChild("Late");
      int late = lateElement.getIntContent();     
      arrayLateTimes.append(late);

      println(" Station: " + fullName + ". Next Train: " + origin + " to " + destination + " due in " + due + "mins / Running " + late + " mins late." );
    } 
  } 
}

Answers

  • edited April 2018 Answer ✓

    You have an array of Arcs.

    Arc[] arcs;
    

    That's good. But how many arcs does each Arc object draw?

    // In class Arc, in display():
    for (int i=0; i<arrayDueTimes.size(); i++) {
      // ...
    }
    

    Oh dear! Each one of the Arcs is redrawing ALL of your Arcs!

    You should modify your Arc class so that it represents (and draws, and otherwise deals with) one SINGLE arc. It should not need the total number of Arc's at all!

  • Thanks, TfGuy44. I tried to follow your tips but failed :( I posted my current code to this thread: https://forum.processing.org/two/discussion/comment/122790#Comment_122790

    koogs reminded me that I should not double post to keep things in context. My bad :(

This discussion has been closed.