944,114 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 398
  • Java RSS
Nov 4th, 2009
0

Taking too long to print out 500x500 2D Array

Expand Post »
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?

java Syntax (Toggle Plain Text)
  1. public void addRowsToGrid()
  2. {
  3. String allRows = "";
  4.  
  5. for(int i = 0;i < 500; i++) {
  6. allRows += "\"";
  7. for(int j = 0;j < 500; j++) {
  8. allRows += grid[i][j].getColor();
  9. }
  10. if (i != 499) {allRows += "\",\n";}
  11. else if (i == 499) {allRows += "\"\n";}
  12. }
  13. allRows += "};\n";
  14.  
  15. XPM += allRows;
  16. }

Can I get some feedback?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
gotm is offline Offline
33 posts
since May 2008
Nov 4th, 2009
0
Re: Taking too long to print out 500x500 2D Array
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!
Last edited by BestJewSinceJC; Nov 4th, 2009 at 8:56 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Correcting inefficiency in pseudocode
Next Thread in Java Forum Timeline: Issue with MouseListener





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC