|
-
Apr 16th, 2003, 08:52 PM
#1
Thread Starter
Hyperactive Member
can this code be optimized?
PHP Code:
import java.io.*;
public class test {
public static void main (String[] arg)throws IOException{
for (int a=2;a<10;a++){
PrintWriter out = new PrintWriter(new FileWriter(""+a+".txt"));
for (int b=0;b<10;b++){
for (int c=0;c<10;c++){
for (int d=0;d<10;d++){
for (int e=0;e<10;e++){
for (int f=0;f<10;f++){
for (int g=0;g<10;g++){
out.println(""+a+b+c+d+e+f+g);
}
}
}
}
}
}
out.close();
}
System.out.println("Done");
}
}
I am wondering to know if this code can be optimized to be faster
thanks
Last edited by proff.hacker; Apr 16th, 2003 at 08:55 PM.
-
Apr 17th, 2003, 05:43 AM
#2
No, not really. If you need 8 million iterations then it doesn't matter whether it's one loop or 7.
You could probably get away with ~3 loops and some very tricky math to come to less iterations, but it takes some effort to work it out.
You could for example unroll the innermost loop, but the compiler should do that for you anyway.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 17th, 2003, 09:45 PM
#3
Addicted Member
Maybe declaring the ints outside of all the loops may help. It will probably be minimal (until you approach 8 million iterations). I'm not sure if primitives will start to take up memory like objects and require garbage collection. (Assuming you don't mean only the specific example you posted).
Then again, if you know the innards of how the compiler optimizes, you are way ahead of me.
Also,
for(i=0; i<someArray.length; i++)
is worse than
theLength=someArray.length;
for(i=0; i<theLength; i++)
-
Apr 18th, 2003, 01:30 AM
#4
Dazed Member
So you are creating eight files filled with eight million values each? The only thing i could see holding the speed back would be using the print() and println() methods each of which convert their arguements to strings before outputting them.
Last edited by Dilenger4; Apr 18th, 2003 at 01:43 AM.
-
Apr 18th, 2003, 02:19 AM
#5
8 files with 1 Mio. strings.
Anyway, the slowest thing about that will still be harddisk access, so don't worry about optimizing. The only efficient optimizing that can be done is buffering output in a large buffer, but that's not under your control.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 18th, 2003, 12:35 PM
#6
Dazed Member
Corned Bee just curious, is PrintWriter buffered? From what i remember you can turn auto flushing on when a PrintWriter is created so by default auto flush should be off. So i guess in his case the stream should be flushed when close() is called rather then the stream being flushed with each subsequent call to println().
-
Apr 19th, 2003, 07:53 AM
#7
I don't know, I don't know that much about Java.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|