Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a need for speed

 
Randall Twede
Ranch Hand
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just added a flood fill feature to my paint program. it works just fine but it is a bit too slow for my liking. after solving 48 euler project problems, i am thinking there must be a way to speed this up. to flood fill most of the screen on my antique laptop takes about 2 minutes.
 
Randall Twede
Ranch Hand
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i solved the problem. i was thinking it was the foreach loop to check each point before adding it to the queue to make sure it is not already there. i was thinking it would be nice if ArrayDeque had a contains() method. then i thought it would have to do the same thing i do so maybe it wouldn't help. when i finally got a chance to check the API, it turns out there is a contains() method. it works about 8 times as fast now
less code too
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:less code too

Maybe, but any time I see 8 "end-block"s in a row, I get brain-ache.

My advice:
1. Put this into a separate method.
2. See if you can't reduce some of those loops and/or if's. I can't remember the last time I wrote code nested 8 deep - or even if I ever have.

Also: Please DontWriteLongLines. It makes your thread very difficult to read. I'd correct them myself, but you have tons of them.
Remember: 80 characters max.

Winston
 
Randall Twede
Ranch Hand
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
putting it in a seperate method would probably be a good idea. the entire mouseReleased() method is pretty long.
as for the nesting, that is just the way this problem gets solved.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:as for the nesting, that is just the way this problem gets solved.

No it isn't - or at least, it's certainly not the only way to solve it.

For starters, if you have two 'if' statements in a row, you can join them with an '&&'.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic