+ Reply to Thread
Results 1 to 5 of 5

Seeking knowledge on VBA speed - which method is faster

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Lightbulb Seeking knowledge on VBA speed - which method is faster

    I have a theoretical question. Say a large procedure is made up of small code chunks/snippets. Would it run slower if these chunks/snippets were placed in separate subs in the same module and called from the original/master sub? (All other factors being equal).

    I am afraid I am not explaining myself very well. I'll try again: Of the two methods below, which would run faster?

    METHOD 1:

    Sub Method1
    code snippet1
    code snippet2
    code snippet3
    etc
    End Sub


    METHOD 2:

    Sub Method2
    Call Snippet1Sub
    Call Snippet2Sub
    etc
    End Sub
    Sub Snippet1Sub
    code snippet1
    End Sub
    Sub Snippet2Sub
    code snippet2
    End Sub
    etc
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Seeking knowledge on VBA speed - which method is faster

    Hello mc84excel,

    Generally speaking, method 1 would be faster. Anytime a procedure must pass arguments to and receive arguments from other calls, there will be an increase in processing time due to switching threads. However, it should be noted that the time lag will be very small. For it to become significant, it would take a 100,000 or more cycles. This is not an absolute as the processor architecture will greatly influence this number.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Seeking knowledge on VBA speed - which method is faster

    Method 2 would be much better programming practice: keep procedures small, atomic, and clear to enhance reusability and simplify maintenance.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Seeking knowledge on VBA speed - which method is faster

    Quote Originally Posted by Leith Ross View Post
    Generally speaking, method 1 would be faster. Anytime a procedure must pass arguments to and receive arguments from other calls, there will be an increase in processing time due to switching threads.
    That's what I suspected. Thank you for confirming this and for providing the cycle count.

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Seeking knowledge on VBA speed - which method is faster

    Quote Originally Posted by shg View Post
    Method 2 would be much better programming practice: keep procedures small, atomic, and clear to enhance reusability and simplify maintenance.
    Agree. I am (mostly-) self taught in VBA and had came to my own conclusion that method 2 was neater and allowed for greater portability of code.

    My only concern was that there would be a speed impact. However, from the answer from Leith Ross, it seems that this method IS slower but the effect is too minor to worry about.
    Last edited by mc84excel; 02-07-2013 at 12:07 AM. Reason: clarify post

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1