• 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

for loop vs inline classes

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both for excellent input, much appreciated.

Legends ;-)
 
reply
    Bookmark Topic Watch Topic
  • New Topic