Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Re: Hashtable-based word count performance (http://www.velocityreviews.com/forums/t124914-re-hashtable-based-word-count-performance.html)

Gordon Beaton 07-28-2003 07:22 PM

Re: Hashtable-based word count performance
 
On 28 Jul 2003 12:06:46 -0700, enclume42 wrote:
> I suspect the following lines to be the cause of this
> poor performance, because of object creation/loss-of-reference/
> garbage-collector-overheating:
>
> if (!ugcounter.containsKey(ug)) {
> ugcounter.put(ug,new Integer(1));
> } else {
> ugcounter.put(ug,new Integer(1+((Integer)ugcounter.get(ug)).intValue()) );
> }


One simple improvement you can make to the above is that you really
don't need to use contains() followed by get(). Use just get(), it
will return the entry if it exists and null otherwise:

if ((prev = (Integer)ugcounter.get(ug)) != null) {
ugcounter.put(ug,new Integer(1+prev.intValue()));
}
else {
ugcounter.put(ug,new Integer(1));
}

If you don't like to create a new Integer object for each increment,
define a class of your own that lets you increment the value it
contains. Then you could do something like this:

if ((counter = (MyCounter)ugcounter.get(ug)) != null) {
counter.increment();
}
else {
ugcounter.put(ug,new MyCounter(1));
}

Also, you should examine the part of your code where you read the file
and parse it into words. I suspect that's where the real bottleneck
is.

That said, you really don't know any of this unless without profiling
the code...

/gordon

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m

enclume42 07-29-2003 11:11 AM

Re: Hashtable-based word count performance
 
Thanks for the advice. Two of them suggested to implement
a mini Counter class, which I did. Unfortunately it has not
improved much...

As I am not reading information from a file but from data
structures preexisting in the memory, Gordon's suggestion to
use a profiler sounds now the thing to do.

Thus, which tools are available to profile my Java code ?

I am working under Linux with J2SDK 1.4.1.


(If NetBeans [3.4.1] has an integrated profiler, I cannot
use it because it complains about one of the constructs in
my code, the latter being recognized as valid when I compile
from the command line...)


Gordon Beaton <not@for.email> wrote in message news:<3f2577e3$1@news.wineasy.se>...

snip snip

> Also, you should examine the part of your code where you read the file
> and parse it into words. I suspect that's where the real bottleneck
> is.
>
> That said, you really don't know any of this unless without profiling
> the code...
>
> /gordon


Roedy Green 07-29-2003 06:55 PM

Re: Hashtable-based word count performance
 
On 29 Jul 2003 04:11:02 -0700, enclume42@hotmail.com (enclume42) wrote
or quoted :

> Thus, which tools are available to profile my Java code ?


see http://mindprod.com/jgloss/profiler.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Roedy Green 07-29-2003 06:58 PM

Re: Hashtable-based word count performance
 
On 29 Jul 2003 04:11:02 -0700, enclume42@hotmail.com (enclume42) wrote
or quoted :

> Thus, which tools are available to profile my Java code ?


the other place you are likely squandering time is in i/o. See
http://mindprod.com/jgloss/buffer.html. For speed, ideally you read
the entire file in one great i/o.

see http://mindprod.com/fileio.html for example code.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


All times are GMT. The time now is 07:26 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.