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 & HelpSyntax Questions › appending array vs initializing with preset length
Page Index Toggle Pages: 1
appending array vs initializing with preset length (Read 456 times)
appending array vs initializing with preset length
Apr 26th, 2008, 9:05pm
 
Hi everyone, i'm dealing with large amounts of vertex information that i'm loading into arrays.  At first i was reading the whole file to obtain the total number of vertices and then initialized the array size with that number.  Later i changed my code to start with an array size of 0 and then used append to add the vertecies one by one into the array. I noticed that for larger arrays this started to take longer than my previous method.  Now that i'm dealing with fairly large files the whole thing freezes up without spitting out any sort of errors.  I'm guessing this has something to do with memory allocation.  Anybody care to fill me in? i've never really used append before and would like to know what the disadvantages are.
Re: appending array vs initializing with preset le
Reply #1 - Apr 26th, 2008, 10:37pm
 
Hi,

Appending to an array (with Processing's append()) is actually creating a new array (with one more element in it),  copying the old one to the new and then throwing out the old one. This means that at some point in time you will actually need twice the space in memory. If your doing this for every element, you're wasting a lot of cpu just copying data and memory will also have to be garbage collected often.

Instead of appending  you should expand the array, when it has run out of space, by N elements (N beeing larger than one; it may be a fixed value or a percentage of the current size).

This is done in class java.util.Vector, so you may consider using it.
Re: appending array vs initializing with preset le
Reply #2 - Apr 26th, 2008, 11:59pm
 
Holy crap no wonder it was taking so long i was trying to import a 6mb .obj file Tongue thats alot of vertices and arrays being copied no wonder it was taking a dump on me. thanks for clearing that up. I'm going to stick with initializing the array to a static number.

muito obrigade.
Re: appending array vs initializing with preset le
Reply #3 - Jul 9th, 2008, 11:04pm
 
Is there a way to do this in Processing without that problem Maybe append needs to be fixed. I wish I could just do push() and pop().

[edit] Oh! You were being literal!
Code:
expand() 


Code:
contract() 



My world is suddenly skittles and rainbows! Cheesy
Page Index Toggle Pages: 1