Results 1 to 12 of 12

Thread: [RESOLVED] What's more efficient?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Resolved [RESOLVED] What's more efficient?

    Say I have two buttons on a form. Both buttons perform relatively the same function, which is displaying a Modal Dialog form. However, each button sends a different set of parameters to the Dialog form to perform different actions.

    So, my question is this. Is it more efficient to have one click event handle both buttons (using sender to determine which button was pressed to send the appropriate parameters), or is it better to have two click events (one for each button). I guess another question would be, is there a difference at all?

    I know it might look a little cleaner to have one event handling both buttons, would make it a tiny bit easier to modify the code in the future since both buttons would be in the same spot, but is there any advantages to using one method over the other?

    This is more for personal knowledge then anything as I personally can't see any performance gain from using one event handler over two.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: What's more efficient?

    Both are same and there is no question of performance gain or loss. It is just a matter of personal prefrence. If both of them have a lot in common, I would put them in same event handler code and differentiate one from the other using Sender argument. Of they have a very little in common to share, I would prefer coding them as two separate event handlers.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: What's more efficient?

    Things like this are usually not a matter of efficiency (even if there was a difference it would likely be too small to even measure) but more a matter of maintainability.

    Let's say you have not two buttons but 15 buttons doing roughly the same thing, calling some method X, and you handle each of them separately. Now, much later in the development, you have to change something and do something else before you call method X. If you handle the 15 click events separately, you have to change 15 event handler methods. If you handled them in one method, you only have to change it once.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: What's more efficient?

    Yah, i figured there wasn't much to gain using one over the other. Although using the one event handler over the other I get to write like 4 lines less of code. Really the only difference is one parameter which determines an account type. Other then that both buttons perform the same function.

    Let's say you have not two buttons but 15 buttons doing roughly the same thing, calling some method X, and you handle each of them separately. Now, much later in the development, you have to change something and do something else before you call method X. If you handle the 15 click events separately, you have to change 15 event handler methods. If you handled them in one method, you only have to change it once.
    Yup, definitely know this as I run into it a lot when handling radio buttons. Makes things a lot neater to be able to change the code all in one place then alter it in 15 different spots. Plus I'm slightly lazy and it's easier typing in the handles clause then generating a code stub for each radio button

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: What's more efficient?

    Technically, the two buttons will be faster because you don't need to change cases based on Sender, but you will never be able to show the difference in practical terms.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: What's more efficient?

    interesting one would think that using slightly less code would be more efficient (even if it's not discernible to the naked eye). I knew that I wouldn't be gaining or losing anything major by choosing one way over the other, just a question that popped into my head when I was writing out the code for the buttons. Couldn't really come up with an answer so i figured someone on here should know

  7. #7
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: What's more efficient?

    I follow this general rule: If you have two or more objects with events that's got the same (or very similar) code, have them all in the same event handler sub.

    If you have 2 or more spots where it's got the same code, move it to a Sub and call that sub from all locations.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: What's more efficient?

    Total lines of code means little or nothing. Consider that when you turn Option Strict ON, you have to type more code to do all those explicit conversions, but your code will run faster. Loop unrolling, which is no longer really practised, was another means of increasing speed by typing more total lines.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: What's more efficient?

    Yah, I'm a pretty big advocate of code reuse. I was really just in the dark about any possible performance gains or losses from using one method over the other. I wasn't really expecting to gain anything major, if anything at all.

    Total lines of code means little or nothing. Consider that when you turn Option Strict ON, you have to type more code to do all those explicit conversions, but your code will run faster. Loop unrolling, which is no longer really practised, was another means of increasing speed by typing more total lines.
    what about compile times? Technically wouldn't a program with more lines take longer to compile then code with less?

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: What's more efficient?

    Probably, but so what? If you managed to get a project of such size that something like this mattered, then you can goof off during that time.
    My usual boring signature: Nothing

  11. #11
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: What's more efficient?

    Quote Originally Posted by Shaggy Hiker View Post
    Probably, but so what? If you managed to get a project of such size that something like this mattered, then you can goof off during that time.
    Ahh, imagine being able to sleep in front of your desk for 3 microseconds longer because you added 3 more lines to your code... What a life...

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: [RESOLVED] What's more efficient?

    It isn't how long it takes, it is how long you SAY it takes.

    Edit: I should add that I think every computer-related comic strip has used this device for at least one strip, if not several.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width