Performance issue with ArrayList

edited June 2016 in Library Questions

Hi everyone,

I am having performance issue with ArrayList. I have spend about a day trying to figure out what's the potential cause but with no success. I was wondering whether anyone could help me. The code is below. What I am trying to do is to create a sphere with points. I want to allow the user to control the number of points through the slider. But when I change the number of points to 2000 the sketch really slows down. Below is the code of my sketch.

Thank you very much for your time.

Best, Frank

import peasy.*;
import controlP5.*;

PeasyCam cam;
ControlP5 cp5;
PMatrix3D currCameraMatrix;
PGraphics3D g3;

float r = 100;
float alpha, beta;
float noiseScale = 0.003;
int numPoints = 100;

Slider pNum;
ArrayList<SPoint> particles = new ArrayList<SPoint>();

void setup() {
  size(600, 600, P3D);
  g3 = (PGraphics3D)g;

  background(0);
  frameRate(60);
  noSmooth();

  cp5 = new ControlP5(this);

  cp5.addSlider("numPoints")
     .setPosition(20, 20)
     .setRange(10, 2000)
     .setCaptionLabel("Number of Points")
     .setHeight(20)
     .setValue(numPoints)
     ;

  cp5.setAutoDraw(false);

  // Setup the camera
  cam = new PeasyCam(this, 250);

  noFill();
  stroke(5, 207, 255);

  // Initialize the points
  for(int i = 0; i < numPoints; i++) {
    particles.add(new SPoint(random(0, TWO_PI), random(0, TWO_PI), r));
  }
}

void draw() {
  pushMatrix();
  background(0);

  // Update the location of the points
  for(int i = 0; i < numPoints; i++) {

    // Decide whether the system needs to remove or add points responding to the slider
    if(numPoints < particles.size()) {
      particles.remove(particles.size()-1);
    } else if (numPoints > particles.size()) {
      particles.add(new SPoint(random(0, TWO_PI), random(0, TWO_PI), r));
    }
    particles.get(i).updateAngles(noiseScale);
    particles.get(i).update();
    println("numPoints: " + numPoints);
    println(particles.size());
  }
  popMatrix();
  gui();
}

void gui() {
  currCameraMatrix = new PMatrix3D(g3.camera);
  camera();
  cp5.draw();
  g3.camera = currCameraMatrix; 
}
Tagged:

Answers

Sign In or Register to comment.