We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › append (arrays) can't catch up
Page Index Toggle Pages: 1
append (arrays) can't catch up? (Read 2455 times)
append (arrays) can't catch up?
Sep 18th, 2007, 11:37pm
 
Hi,

I wrote some simple code, which allows you to draw with the mouse. I store the mouse coordinates in arrays because I'd like to do some stuff with that info later..
But, somehow if you draw fast the lines are disrupted. I probably has something to do with an inefficient way of storing these coordinates? (;too heavy demands on memory, or somethin'?)

Here's my Code:

Line lines;

void setup() {
 
 size(600, 300);
 background(255);
 
 lines = new Line();
 
}

void draw() {
 lines.drawLine();
}

class Line {
 
 int[] xCoordinates = new int[0];
 int[] yCoordinates = new int[0];
 int index = -1;
 
 Line() {
   
 }
 
 void appendLineProps(int x, int y) {
   xCoordinates = append(xCoordinates, x);
   yCoordinates = append(yCoordinates, y);
   index++;
 }
 
 void drawLine() {
   if(index > 1) {
     if(xCoordinates[(index-1)] != -1 && xCoordinates[index] != -1) {
       line(xCoordinates[(index-1)], yCoordinates[(index-1)], xCoordinates[index], yCoordinates[index]);
     }
   }
 }
 
}

void mouseReleased() {
 lines.appendLineProps(-1, -1);
}

void mouseDragged() {
 lines.appendLineProps(mouseX, mouseY);
}

I appreciate your help,

BEnm  


Re: append (arrays) can't catch up?
Reply #1 - Sep 18th, 2007, 11:47pm
 
Unfortunately append gets slower the larger the array you're adding to.

One option is to create a large empty array initially, and have a "lastPoint" int which points to the latest entry in the array. Then if you fill that array, double the size of it, and carry on as before.
Re: append (arrays) can't catch up?
Reply #2 - Sep 19th, 2007, 12:02am
 
Hi JohnG,

thanks for the help.
Do you know some nice resources of information about the behind-the-scenes stuff with arrays?

e.g. I also wonder whether replacing an array - declaring one with the same name (twice as long) - really replaces it, and not only its name; that both keep consuming memory..

thanks
Re: append (arrays) can't catch up?
Reply #3 - Sep 19th, 2007, 12:20am
 
The behind the scenes problem is that append created new array of size original+1, then copies the contents of the original intot he new one, and adds the new item as the last entry.

As you can see, doing this each and every frame isn't very quick.

As for the replacing of it, doing:
Code:
int[] foo=new int[10];
foo=new int[11];


Doesn't delete the original foo, it does however mark it for deletion by the garbage collector, and it will eventually be removed when Java needs some space.
Re: append (arrays) can't catch up?
Reply #4 - Sep 19th, 2007, 7:24am
 
JohnG

Wouldn't you rather just recommend ArrayList?

I wouldn't recommend getting way too comfortable with these
http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

However they are very very useful and does all the behind the scenes stuff for you, as JohnG described.

I've actually written my own ArrayList-like array class, but Java's is still faster.
Re: append (arrays) can't catch up?
Reply #5 - Sep 19th, 2007, 9:58am
 
I would for many cases, but for storing/retrieving ints regularly, I wouldn't, since they have to be boxed/unboxed to/from Integer each and every time you read/write to the ArrayList.
Re: append (arrays) can't catch up?
Reply #6 - Sep 19th, 2007, 1:47pm
 
the fastest when dealing with an array of ints or floats is to use expand(), an example is in this thread:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1187580155
Page Index Toggle Pages: 1