This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Performance and the fly likes Significant difference between if:loop and loop:if? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Significant difference between if:loop and loop:if?" Watch "Significant difference between if:loop and loop:if?" New topic
Author

Significant difference between if:loop and loop:if?

Karl Beowulph
Ranch Hand

Joined: May 31, 2004
Posts: 130
Howdy.

I'm developing a service class for my datagrid and have a performance question.

When the developer is building the grid, they specify the type of datagrid they want as one of the parameters. When in the construction method of the service class I currently have it like this:




(1) - Common for all datagrids
(2) - Common for all datagrids, but has to be in the loop
(3) - Unique to each datagrid type

Each dataGrid type would have it's own IF statment and code segment in the (3) section.

What I'm considering is the following


This would cut down on the lines of code and slightly simplify development, but I'm concerned with the performance hit I'll get by constantly cycling through the IF - ELSE IF's.

There are going to be about 20 TYPEs and the list could potentially have thousands of records.

Any thoughts?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Can you make "type" be an object (a Typesafe Enum perhaps) and move the behaviour that differs into it, calling it polymorphically from the loop?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Karl Beowulph
Ranch Hand

Joined: May 31, 2004
Posts: 130
Enums are in Java 1.5 correct? I can't use that as the server is still on 1.4.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
If I was doing this I would probably use a switch / case type of structure switching on an int type. Those table driven switch structures are pretty fast.
With 20 datagrid types a switch would cut out a lot of if statements.
Bill
[ June 17, 2005: Message edited by: William Brogden ]
Ranga Kalidindi
Greenhorn

Joined: Mar 04, 2005
Posts: 17
This might help as well.



In the construction class of the service class:




-Ranga

[fixed closing code tags - Ilja]
[ June 21, 2005: Message edited by: Ilja Preuss ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Karl Beowulph:
Enums are in Java 1.5 correct? I can't use that as the server is still on 1.4.


The pattern can also be implemented before 1.5, although it's a little bit more work. Basically it's a variation on the Strategy/Command patterns. See "Replace Enums with Classes" at http://java.sun.com/developer/Books/shiftintojava/page1.html
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

To switch, it would be sufficient, to have Datagrid.TEST_TYPE be of type int.
Thousands of records doesn't sound much, so I wouldn't expect much performance difference.


http://home.arcor.de/hirnstrom/bewerbung
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stefan Wagner:
To switch, it would be sufficient, to have Datagrid.TEST_TYPE be of type int.
Thousands of records doesn't sound much, so I wouldn't expect much performance difference.


The advantage of the Typesafe Enum pattern is better flexibility - especially if you need to have more than one switch over the same enum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Significant difference between if:loop and loop:if?
 
Similar Threads
Head First Design Patterns - Factory Method Question (?)
Performance of Java Object Referece
subclasses and access modifiers
How to Return Javascript Alert
Type cast to Class Object