• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Significant difference between if:loop and loop:if?

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic