File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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
Author

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
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

"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: http://aspose.com/file-tools
 
subject: Return number of characters in a Word