Results 1 to 8 of 8
 10242011, 04:56 PM #1Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Avoiding ForLoops (like in MATLAB)?
I know that in some languages like MATLAB, it is more efficient to use matrices instead of for loops where possible.
For instance say I am interested in calculating the following sum:
28*1000+1056*100+898*10+65,
then in MATLAB it would be more efficient to do the following:
x = [28,1056,898,65];
y =10.^[length(x)1:1:0];
z = sum(x.*y);
instead of running a for loop.
Does a similar concept exist in Java?
 10242011, 05:48 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,321
 Blog Entries
 7
 Rep Power
 25
Re: Avoiding ForLoops (like in MATLAB)?
The only person who got everything done by Friday was Robinson Crusoe.
 10242011, 05:50 PM #3Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Re: Avoiding ForLoops (like in MATLAB)?
um, what do you mean by (anonymous) local classes?
 10242011, 05:56 PM #4
Re: Avoiding ForLoops (like in MATLAB)?
His point was that Java has things that matlab doesn't, and vice versa.
But I'm not even sure what you're asking, since I don't really know what the code is doing under the hood in matlab. One rule that I like is that premature optimization is evil. Don't sacrifice maintainability for some perceived gain in efficiency.How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 10242011, 06:07 PM #5Member
 Join Date
 Oct 2011
 Posts
 83
 Rep Power
 0
Re: Avoiding ForLoops (like in MATLAB)?
I've never used matlab, but my guess would be that it has some additional overhead associated with for loops, and therefore provides an alternative to them which avoids that overhead. Java, as far as I know, has no significant overhead associated with for loops, so there's no problem just using them.
 10242011, 06:12 PM #6Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Re: Avoiding ForLoops (like in MATLAB)?
I see. I actually thought that matlab maybe using some sort of method for parallel computation, which is quicker than a simple forloop over a multidimensional matrix (although my example is just over a 1D array). Anyway, it made me wonder whether the same holds for other languages ...
 10242011, 06:12 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,321
 Blog Entries
 7
 Rep Power
 25
Re: Avoiding ForLoops (like in MATLAB)?
Maybe in Matlab explicit loops are slower because Matlab has to interpret the whole shebang; implicit loops (like in dot producs for vectors) are much faster. In Java an explicit loop is as fast as can be:
Java Code:double[] x = { 28,1056,898,65 }; double sum= 0; int factor= 1000; for (int i= 0; i < x.length; i++, factor/= 10) sum+= factor*x[i];
Jos
ps. and Java has much much more that Matlab doesn't have and vice versa.The only person who got everything done by Friday was Robinson Crusoe.
 10242011, 06:15 PM #8Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Re: Avoiding ForLoops (like in MATLAB)?
[QUOTE=JosAH;241231]Maybe in Matlab explicit loops are slower because Matlab has to interpret the whole shebang; implicit loops (like in dot producs for vectors) are much faster. In Java an explicit loop is as fast as can be:
Java Code:double[] x = { 28,1056,898,65 }; double sum= 0; int factor= 1000; for (int i= 0; i < x.length; i++, factor/= 10) sum+= factor*x[i];
Yes, I know, which is the reason why I'm coding in Java :)
Similar Threads

Avoiding allocations
By Skiller in forum New To JavaReplies: 7Last Post: 02022011, 01:26 PM 
A clever way of doing this ... (avoiding a LOT of ifelse statements)
By doejohn in forum New To JavaReplies: 3Last Post: 07012010, 08:39 PM 
avoiding memory overconsuming
By itaipee in forum New To JavaReplies: 4Last Post: 12142009, 12:59 PM 
Avoiding refresh
By java_srinivasan in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 06252008, 09:01 AM 
avoiding if statements
By valoyivd in forum New To JavaReplies: 1Last Post: 04022008, 09:08 AM
Bookmarks