wood burning stoves 2.0*
The moose likes Performance and the fly likes for loop vs inline classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "for loop vs inline classes" Watch "for loop vs inline classes" New topic
Author

for loop vs inline classes

Peter Nicholson
Greenhorn

Joined: Feb 11, 2012
Posts: 6
Hello everyone,

I've been doing some experimenting with multiple buttons and action listeners. The buttons (there's 10 of them) are stored in an array.

Each button has an action listener registered i.e. (this).

I'm only running one actionPerformed method which loops through the array listening for a button :


My question is - is this an efficient way to deal with multiple buttons and listeners, or should I instead be using inner classes and implementing multiple listeners?

Are there any downfalls to the way I am doing this ?

Thanks :-)

- edit - code tags added - fbr
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

In the future, when posting code, please UseCodeTags(⇐click) so it will be readable.

I can tell you without even reading your code, however, is that these tiny microoptimizations will almost never make any noticeable difference, and definitely never in the case where user interaction is involved. Even for very fast user operation of your app and a very slow computer, we're talking, worst case, about maybe 100 ms for a user action vs. maybe 10 ns for the difference in the execution times.

In other words, the user's reaction times will be at least 10,000,000 times slower than any difference in the code's execution speed. So it's like going from a time of 1.00000001 vs. 1.00000002

And realistically, the difference will be much, much, less, and quite possibly nothing at all.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

I totally agree with Jeff.

Anyway, I'd suggest an altogether different way here. I usually create an Action for every command a user can initiate (at least in more complicated forms). Single action can then be used to create a toolbar button and a menu item. Clicking the button or selecting the menu item then triggers the same action, and moreover, If I disable the action, both button and menu item get disabled too. A checked/unchecked state can be linked via the action to all relevant UI elements, which is even more useful. In other words, it allows to separate the action and its state from its manifestation in the user interface.

Actions are cool, try to look into them. It might take some time to get used to them, though.
Peter Nicholson
Greenhorn

Joined: Feb 11, 2012
Posts: 6
Thanks both for your replies, and I'll be sure to use code tags in the future.

If I may digress a little, how do you go about testing the efficiency of your code ? (please advise if I should start a new post) i.e. if you are trying to compare different methods of achieving the same goal, how would one "capture" elapsed time for a piece of code to execute ?

Obviously as you've pointed out, the example I've posted is not worth comparing, however it would still be beneficial to know how to compare if required.

Thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Peter Nicholson wrote:if you are trying to compare different methods of achieving the same goal, how would one "capture" elapsed time for a piece of code to execute ?


For "long" operations--say, a second or more:

1. Get the start time.
2. Run the operation (preferably many times).
3. Get the end time.
4. Subtract the times.
5. Divide by number of runs in step 2.

For faster operations, or when trying to find a bottleneck, use a profiler.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

Jeff Verdegan wrote:For "long" operations--say, a second or more:
1. Get the start time.
2. Run the operation (preferably many times).
3. Get the end time.
4. Subtract the times.
5. Divide by number of runs in step 2.

I'd add another point:
6. Take whatever number you arrive at with a big grain of salt, since it is well possible, or even probable, that JVM has recompiled your code right in the middle of your benchmark. In other words, don't expect the accuracy to be down to the millisecond.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Martin Vajsar wrote:
Jeff Verdegan wrote:For "long" operations--say, a second or more:
1. Get the start time.
2. Run the operation (preferably many times).
3. Get the end time.
4. Subtract the times.
5. Divide by number of runs in step 2.

I'd add another point:
6. Take whatever number you arrive at with a big grain of salt, since it is well possible, or even probable, that JVM has recompiled your code right in the middle of your benchmark. In other words, don't expect the accuracy to be down to the millisecond.


And of course:

7 (or maybe 6a). Keep in mind that you measured that operation in that environment on that hardware at that time with those inputs, etc., etc., etc. A single number or handful of numbers doesn't necessarily tell you anything. Unless you're actually taking a statistically valid number of samples across an appropriate cross-section of the "population" of runtime context, and looking at the distribution of the values, you need to take the result with a big grain of salt.

In other words, be aware of what you're measuring, and be aware of the limitations of that measurement for both analysis and prediction.
Peter Nicholson
Greenhorn

Joined: Feb 11, 2012
Posts: 6
Thank you both for excellent input, much appreciated.

Legends ;-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop vs inline classes