+ Reply to Thread
Results 1 to 6 of 6

execution speed comparrision

  1. #1
    Forum Contributor
    Join Date
    02-05-2015
    Location
    czech
    MS-Off Ver
    2010
    Posts
    172

    execution speed comparrision

    Is there a page or book which compares methods by speed?

    For example I'm interested in following:
    -I have 2 arrays or collections (list LA and list LB)
    -I have 2 things (thing ta and thing tb) or more which I want to apply on every member of lists

    -list can be array of strings or numbers, non empty range of cells
    -array or range is constructed during loop (if condition)
    -things to apply: text size or color, number or string manipulation

    I can do following:
    case1
    1. loop through list LA and apply thing ta and thing tb simultaneosly
    2. loop through list LB and apply thing ta and thing tb simultaneosly
    (in all 2 consecutive loops)

    case2
    1. loop through list LA and LB simultaneously and apply thing ta
    2. loop through list LA and LB simultaneously and apply thing tb
    (in all 2 consecutive loops)

    case3
    1. loop through list LA and LB simultaneously and apply thing ta and tb simultaneously
    (1 loop)

    case4
    1. loop through list LA and apply thing ta
    2. loop through list LA and apply thing tb
    3. loop through list LB and apply thing ta
    4. loop through list LB and apply thing tb
    (in all 4 consecutive loops)

    Which of the cases is fastest?

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: execution speed comparrision

    One way is just to use Timer on all of your cases.

    As the first and last lines in your subs, use:

    Please Login or Register  to view this content.
    Or however you want to display the results, which will be in seconds.
    I'm interested in starting a career working with VBA, if anyone knows of any opportunities!

  3. #3
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: execution speed comparrision

    Quote Originally Posted by jakopak View Post
    -list can be array of strings or numbers, non empty range of cells
    -array or range is constructed during loop (if condition)
    -things to apply: text size or color, number or string manipulation
    Generally, it is better to iterate once and do as much as possible in each iteration.

    This is especially true if you are modifying cell properties (like formatting).

    Each access to an Excel cell or range is very expensive because it involves "interprocess" (really interthread) communication. Not only does the interprocess communication take a lot more time than a VBA function call, for example, but also it involves a process context switch, which allows other processes on the computer to take control of the CPU.

    (The context switch issue might be less of a concern with modern multiple-core computers. I still use a single-core single-CPU computer. But the interprocess communication overhead is still an issue.)

    For that reason, if you are manipulating cell values, not properties, it is usually best to read a range into a VBA variable, do all the manipulation, then write the VBA variable out to the Excel range. For example:
    Please Login or Register  to view this content.
    That said, there are situations where it might be better to iterate on each VBA array separately, namely: if you have very large VBA arrays, each with many thousands of elements that you are accessing sequentially.

    In that case, the benefit of iterating each VBA array separately arises from the "locality" of VBA address references. Each time you access a single element in a VBA array, the CPU actually loads many neighboring elements into the cache as well, which takes some amount of time. If the next reference is to a neighboring array element that is already in the cache, it is faster for the CPU to access it.

    Usually, I do not lose sleep over "cache optimization". But I have encountered situations where that makes a significant difference in overall performance.
    Last edited by joeu2004; 10-25-2015 at 06:35 PM. Reason: minor code change

  4. #4
    Forum Contributor
    Join Date
    02-05-2015
    Location
    czech
    MS-Off Ver
    2010
    Posts
    172

    Re: execution speed comparrision

    Joeu2004, thank you very much. Excellent answer I hopped for.

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: execution speed comparrision

    To clarify though, there are probably 101 things that will make a far larger difference to performance than that level of optimization that you'd start with - it's probably worth posting your code and asking for feedback

  6. #6
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: execution speed comparrision

    Quote Originally Posted by Kyle123 View Post
    To clarify though, there are probably 101 things that will make a far larger difference to performance than that level of optimization that you'd start with
    I second that emotion.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Speed up this macros execution
    By banaanas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2015, 01:58 AM
  2. Want to Speed Up Code Execution
    By PosseJohn in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-28-2013, 07:34 PM
  3. Execution speed of macros
    By JM967 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-25-2013, 02:43 PM
  4. [SOLVED] Execution Speed of VBA Code
    By sdelaney7 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-17-2012, 08:35 PM
  5. how to speed up macro code execution?
    By sharmanjali87 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-03-2010, 06:45 AM
  6. [SOLVED] Speed up Excel execution
    By Sinus Log in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-08-2005, 01:10 AM
  7. code execution speed question
    By Gary Keramidas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-24-2005, 05:05 PM

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