Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes Return number of characters in a Word Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Return number of characters in a Word" Watch "Return number of characters in a Word" New topic

Return number of characters in a Word

Raihan Jamal
Ranch Hand

Joined: Mar 23, 2010
Posts: 86
Can we optimize this code more in a more efficient way?

Rob Spoor

Joined: Oct 27, 2005
Posts: 20193

It would be more efficient to not split the String, but use one single loop over the entire String. Use a few variables to keep track of the current state; a new word is started when the previous character is whitespace etc, and a word ends when the current character is whitespace etc. It's a bit trickier to get it right, so perhaps you should keep your current version. If you can improve efficiency at the cost of readability (by yourself in the first place) then you shouldn't do it (unless you need the extra efficiency because your resources are limited).

How To Ask Questions How To Answer Questions
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 8837

Raihan Jamal wrote:Can we optimize this code more in a more efficient way?

I think, rather than worrying about the few nanoseconds you might save with a fully optimized piece of code, you should concentrate on the inconsistencies you have with your current one.

For example, your "letter counting" method uses the generic Character.isLetter(), but your split() only allows for English punctuation (and not all of that either).

Get the code right first. Then worry about efficiency.

And I'd say that the first step to getting it right is to write down, in English, exactly what you expect it to do.


Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11923

"more efficient" is not a well defined term. More efficient in terms of memory? speed? ease of understanding/refactoring?

just about everything in CS is a trade-off. faster execution may require more memory - a potential problem in mobile devices. Developers tend to spend more time fixing/correcting/debugging their code than anything else so writing code that is easy to understand usually saves more time.

Think about it...if you do something that saves a user a microsecond, then a million users will have to run that segment of code to save ONE SECOND.

However, if you write it in such a complicated way that it takes you eight hours to debug, test, package up, and distribute the patch, you have ended up 6 hours, 59 minutes and 59 seconds in the hole.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Return number of characters in a Word
jQuery in Action, 3rd edition