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 › arraycopy / subset / append -- which is fastest
Page Index Toggle Pages: 1
arraycopy / subset / append -- which is fastest? (Read 682 times)
arraycopy / subset / append -- which is fastest?
Oct 21st, 2007, 10:15pm
 
hi all

can anyone who knows what's going at the heart of p5 tell me which of these two would the fastest way to shift an array over by 1, and fill in a value (basically using it keep a running average):

len = hp.length;
hp = append(subset(hp, 1, len - 1),f);

or

len = hp.length;
arraycopy(hp,1, hp,0,len-1);
hp[len-1] = f;

thanks!
Re: arraycopy / subset / append -- which is fastes
Reply #1 - Oct 21st, 2007, 10:54pm
 
append() and subset() are slow(ish), arraycopy is fast. i can't remember whether arraycopy works if the source and destination array are the same, you'll have to check. if not, it might be for() loop time.

fastest of all is to use % (modulo) to move through the array, rather than shifting it over.
Re: arraycopy / subset / append -- which is fastes
Reply #2 - Oct 22nd, 2007, 1:49am
 
great, thanks fry

(for others: modulo technique is summarised here:

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Programs;action=display;num=1185202099;start=2#2

)
Re: arraycopy / subset / append -- which is fastes
Reply #3 - Oct 22nd, 2007, 6:20pm
 
ch wrote on Oct 21st, 2007, 10:15pm:
hi all

can anyone who knows what's going at the heart of p5 tell me which of these two would the fastest way to shift an array over by 1, and fill in a value (basically using it KEEP A RUNNING AVERAGE):

...

thanks!


This was a trick question!

The fastest way to keep a running average is to avoid shifting the array elements completely.  Just treat it like a circular array and keep an index to the "last" element.  Just increment this index each time, and wrap around when you get to the end of the array.

-- djones
Page Index Toggle Pages: 1