| | |
Taking too long to print out 500x500 2D Array
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 0
So one of the problems I have been having with my programs is the length of time it takes to print out a pixel map into the XPM file.
I tested it with like :
500 rows and 2 columns -- 1 second
500 rows and 10 columns -- a few seconds
500 rows and 200 columns -- about 20 seconds
When I try and output the full 500x500 grid after about 4 minutes it's still not done.
Here is the only method I have been using to do this, maybe there is a more efficient way of doing it that I don't know about?
Can I get some feedback?
I tested it with like :
500 rows and 2 columns -- 1 second
500 rows and 10 columns -- a few seconds
500 rows and 200 columns -- about 20 seconds
When I try and output the full 500x500 grid after about 4 minutes it's still not done.
Here is the only method I have been using to do this, maybe there is a more efficient way of doing it that I don't know about?
java Syntax (Toggle Plain Text)
public void addRowsToGrid() { String allRows = ""; for(int i = 0;i < 500; i++) { allRows += "\""; for(int j = 0;j < 500; j++) { allRows += grid[i][j].getColor(); } if (i != 499) {allRows += "\",\n";} else if (i == 499) {allRows += "\"\n";} } allRows += "};\n"; XPM += allRows; }
Can I get some feedback?
•
•
Join Date: Sep 2008
Posts: 1,637
Reputation:
Solved Threads: 206
0
#2 Nov 4th, 2009
Because Strings are immutable, so every time you say string+= in the background, another String is being created and all of the contents are copied into memory. This process takes a lot of time once the String gets large. Use the StringBuilder class instead.
P.S. I made that mistake on a graded project in the past - very embarrassing when my program ran quickly and properly on small to medium sized input files but then when a long novel such as, I don't know, the Iliad is supplied, my project didn't finish fast enough for the TA to care (within 30 seconds as I later learned). But it sure taught me to pay more attention to little details and also to test for more input conditions before assuming I was finished!
P.S. I made that mistake on a graded project in the past - very embarrassing when my program ran quickly and properly on small to medium sized input files but then when a long novel such as, I don't know, the Iliad is supplied, my project didn't finish fast enough for the TA to care (within 30 seconds as I later learned). But it sure taught me to pay more attention to little details and also to test for more input conditions before assuming I was finished!
Last edited by BestJewSinceJC; Nov 4th, 2009 at 8:56 pm.
Out.
![]() |
Similar Threads
- later array initialization (C++)
- Array print problem (C)
- Image Array Issue..... Any ideas??? (Java)
- returning array from ajax.responseText? (JavaScript / DHTML / AJAX)
- Print the parallel array index with the largest value? (C++)
- Dynamic Array, Writing to CSV, Floating Point ?'s (C)
- Pointers (C++)
Other Threads in the Java Forum
- Previous Thread: Correcting inefficiency in pseudocode
- Next Thread: Final Year Project! Please help!
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream






