Re: Hashtable-based word count performance

Discussion in 'Java' started by Gordon Beaton, Jul 28, 2003.

  1. 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
    Gordon Beaton, Jul 28, 2003
    #1
    1. Advertising

  2. Gordon Beaton

    enclume42 Guest

    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 <> wrote in message news:<3f2577e3$>...

    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
    enclume42, Jul 29, 2003
    #2
    1. Advertising

  3. Gordon Beaton

    Roedy Green Guest

    Roedy Green, Jul 29, 2003
    #3
  4. Gordon Beaton

    Roedy Green Guest

    On 29 Jul 2003 04:11:02 -0700, (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.
    Roedy Green, Jul 29, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Brogden

    Re: Hashtable-based word count performance

    William Brogden, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    525
    William Brogden
    Jul 29, 2003
  2. Roedy Green
    Replies:
    2
    Views:
    652
    Roedy Green
    Aug 2, 2003
  3. zer0frequency
    Replies:
    2
    Views:
    739
    andrewh1
    Jul 10, 2004
  4. Kevin
    Replies:
    16
    Views:
    8,202
    Dale King
    Apr 19, 2005
  5. efelnavarro09
    Replies:
    2
    Views:
    914
    efelnavarro09
    Jan 26, 2011
Loading...

Share This Page