my dog learned polymorphism
The moose likes Beginning Java and the fly likes method takes longer each time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method takes longer each time" Watch "method takes longer each time" New topic

method takes longer each time

Masha Stekker

Joined: Mar 19, 2004
Posts: 15
I'm still stuck with my extending loops, despite a lot of help already received on this forum.
I've narrowed down the problem somwhat;
Each time the method that displays the arrays gets called it takes longer to complete. This is bad enough that the program slows down to a complete halt. And its supposed to repeat the loop 1000 times for arrays of 100x100, I'm just testing it at 100 times for arrays of 50x50.
It seems as though it needs to check through more & more information each time. But I dont understand why this might be.
I've added little print out comments at the beginning and end of the methods so that one can see how they slow down.
If I comment out the method that creates and initialises the arrays that get displayed, this makes no difference: so it cant be that I'm re-initialising the arrays and that this takes up more memory. Can it? I'm not sure.

[ April 17, 2004: Message edited by: Jim Yingst ]
kelly devon
Ranch Hand

Joined: Apr 15, 2004
Posts: 77
check ur code againi think u reinitialized row of array2 twice i saw a repeat of that check it out and try again

Masha Stekker

Joined: Mar 19, 2004
Posts: 15
Cant spot it. Whereabouts?
ALso, if I sidestep the whole "display in a scrollable text area" then the thing seems to work (if I simply print the arrays out in MSDos window.)
Why would displaying it in the window make the loops slow down? Is it using up memory somehow the way I'm doing it?
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Hi Masha. I added [code] tags to your post above, since that allows our forum software to preserve the indentation of your code when it's displayed, which makes if more readable. Note that using tabs for indentation is probably a bad idea, as in a web browser it translates into 8 spaces. That's much more indentation than we really need, and it causes your program to use up a lot of horizontal space. Which will make it harder to fit on the screen at once (for people with smaller monitors), which again will make it harder to read. I recommend using spaces for indentation, no more than 4 per level. (4 seems to be the industry standard, though 2 and sometimes 3 are also used).
As for your program - try printing out the value of output after each time you call writeArray(). Look closely at the output from the first few iterations - is it what you expect? How is it different? Is all the output really necessary? Good luck...

"I'm not back." - Bill Harding, Twister
Masha Stekker

Joined: Mar 19, 2004
Posts: 15
Oh I THOUGHT it was something like that!
OH COOOOOOL its finally working. Thanks so much for pointing that out to me!
For any other begginers out there that might ever read this, I changed my code so that in the writeArray method, output is set to be an empty string every time the method starts. Then I changed the bit of code that puts the output in the text area from "outputArea.setText(game3.output);" to "outputArea.append(game3.output);" and moved it inside the loop that makes the method call 500 times.
It seems to work! But I must add, that I've thought that so many times now I'll have to test the thing some more before I can really relax.
Thanks also for the tips on indenting - thats been bothering me.
I agree. Here's the link:
subject: method takes longer each time
It's not a secret anymore!