• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Significant difference between if:loop and loop:if?

 
Karl Beowulph
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Karl Beowulph
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic