+ Reply to Thread
Results 1 to 18 of 18

Which option is faster? Passing a 2D Array

  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

    Which option is faster? Passing a 2D Array

    Suppose I was to write code that would dynamically build a 2D array. The module would contain a number of subprocedures called by the main Sub. The subprocedures would populate the output array (using Redim Preserve)

    Which of the two options below would run faster/complete first?
    1. Pass the array to each subprocedure as a ByRef argument
    2. Create a module level variable to hold the array
    *******************************************************

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

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

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

    Re: Which option is faster? Passing a 2D Array

    I doubt it will make any difference, you're just passing a pointer, I wouldn't be suprised if the compiler outfitted the same code for both scenarios. Your bottleneck is using redim preserve (in it's native form anyway).

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Which option is faster? Passing a 2D Array

    I doubt there is a significant difference in execution speed between the two.

    On another note; ReDim Preserve is expensive (especially with large arrays of Strings or Variants). If possible, calculate the size you need before declaring the array size, or Dimming larger than you will need and Redim Preserve smaller after populating the array.

    This link is full of relevant info and tips.
    Passing And Returning Arrays With Functions
    Last edited by AlphaFrog; 06-22-2016 at 02:06 AM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  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: Which option is faster? Passing a 2D Array

    Quote Originally Posted by AlphaFrog View Post
    ReDim Preserve is expensive (especially with large arrays of Strings or Variants). If possible, calculate the size you need before declaring the array size, or Dimming larger than you will need and Redim Preserve smaller after populating the array.
    Thanks for the tip AlphaFrog.

    I dont think that will be possible unfortunately (I am scraping a website. I know the number of searches before I start but each search will return a varying number of records that I will want to record).

    Interesting idea about dimming larger than what I would possibly need - I will have to try that.
    Last edited by mc84excel; 06-22-2016 at 07:49 PM. Reason: correct grammar, missing bracket

  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: Which option is faster? Passing a 2D Array

    Quote Originally Posted by Kyle123 View Post
    Your bottleneck is using redim preserve (in it's native form anyway).
    Good to hear from you again Kyle. I'm not sure I understand what you mean by the native form of Redim Preserve though.

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

    Re: Which option is faster? Passing a 2D Array

    Pretty much what AlphaFrog said you need to redim preserve larger than you need. Though why not use a collection/dictionary if you don't know the size you need? It's what they're for

  7. #7
    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: Which option is faster? Passing a 2D Array

    Quote Originally Posted by Kyle123 View Post
    Pretty much what AlphaFrog said you need to redim preserve larger than you need.
    Got it. Thanks

    I have started to change the code so that, for each search made, it adds the minimum number of rows possible for that search results. Just this change alone and I have already noticed a significant speed increase. I never knew that Redim Preserve was such a memory hog!

    I am eventually going to change this code to the 'redim larger than ever would be needed at the start' but then I will have to work out the most efficient way to scrub all the empty rows.


    Quote Originally Posted by Kyle123 View Post
    Though why not use a collection/dictionary if you don't know the size you need? It's what they're for
    Well I guess because I am not familiar with them and also because I read somewhere that they are not a lot faster as they are, so to speak, the equivalent of two arrays (the data and the index)? I could be confused on that point though.

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Which option is faster? Passing a 2D Array

    Quote Originally Posted by mc84excel View Post
    I am eventually going to change this code to the 'redim larger than ever would be needed at the start' but then I will have to work out the most efficient way to scrub all the empty rows.
    Just keep a running count each time you add a row to the large array. Then after all rows have been added, ReDim Preserve to the counter size. Keep in mind you can only ReDim Preserve the last dimension of an array. So your 2D array will have to be transposed e.g; MyArray(columns, rows)
    Last edited by AlphaFrog; 06-23-2016 at 08:02 PM.

  9. #9
    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: Which option is faster? Passing a 2D Array

    Quote Originally Posted by AlphaFrog View Post
    Just keep a running count each time you add a row to the large array. Then after all rows have been added, ReDim Preserve to the counter size.
    Good idea! Thanks for that. (Not sure why I didn't think of that as I was intending to add a module level counter anyway to track which row was ready to populate next!)

    Quote Originally Posted by AlphaFrog View Post
    Keep in mind you can only ReDim Preserve the last dimension of an array. So your 2D array will have to be transposed e.g; MyArray(columns, rows)
    Yes. When I populate an array of unknown size, I transpose before I start then transpose back before I output the results. (A long time ago when I was learning arrays, I used to transpose before/after each row I wanted to add. OUCH )

  10. #10
    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: Which option is faster? Passing a 2D Array

    Hmmm. Something went wrong.

    The original code used to process ~25-30 searches per minute. After it was pointed out that Redim Preserve is expensive, I reduced the use of it by adding several rows at the start of each search (and then repeated add one for each additional result needed for that search). This change improved the entire execution to ~40 searches per minute. I was so pleased I decided to bite the bullet and reduce the use of Redim Preserve as far as possible (Redim oversize at the start and then one Redim Preserve at the end to delete the unused). After I did that, the speed DROPPED to 15 searches per minute!

    It is now slower than what it was originally!!! How is this possible? Is there a point where an oversized array becomes more expensive than using repeated Redim Preserve?

  11. #11
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Which option is faster? Passing a 2D Array

    Hard to say without knowing more about the procedure and how you configured things. I don't know what a "search" is within your context. Also, it seems 40 searches per minute is quite slow, but again, don't know what a "search" is.

    Perhaps it would be best to to describe what you are doing and to show your code.

  12. #12
    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: Which option is faster? Passing a 2D Array

    Thanks AlphaFrog. I am scraping a website. I know the number of searches before I start but each search will return a varying number of records that I will want to record. I don't want to show my code for this question. I am just curious if there is any theory/'rule of thumb' out there that governs how to best reduce the use of Redim Preserve?

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

    Re: Which option is faster? Passing a 2D Array

    If you're scrapin, I highly doubt that the redim preserve is the bottle neck, I'd be looking at the server response time

  14. #14
    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: Which option is faster? Passing a 2D Array

    Quote Originally Posted by Kyle123 View Post
    If you're scrapin, I highly doubt that the redim preserve is the bottle neck, I'd be looking at the server response time
    Well I don't have any control over the response times so there is no point getting upset over the aspect that you cant control!
    1. BEFORE = ~25-30 searches per minute
    2. CHANGE 1 - REDUCE FREQUENCY OF REDIM PRESERVE = ~40 searches per minute
    3. CHANGE 2 - DRASTICALLY REDUCE REDIM PRESERVE (through oversized arrays at start) = ~15-20 searches per minute

    Unless I am missing something, it appears to me that the Redim Preserve is the main problem. The first code change to Redim Preserve made a major improvement in running time. (I am used to scraping taking a long time so 40 RPM is a decent result for me. The most I am hoping to achieve is 50-60 RPM)

    However, after I went further (change 2), everything went downhill. It ended up worse than what it was before. I am trying to figure out why.

    My scraping class probably does need an expert to point out amateur mistakes I have made. However I don't see this as a contributing factor to these results as I only changed the Redim Preserve & array aspects.

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

    Re: Which option is faster? Passing a 2D Array

    If you post your scraping code, I'm happy to take a look

  16. #16
    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: Which option is faster? Passing a 2D Array

    Quote Originally Posted by Kyle123 View Post
    If you post your scraping code, I'm happy to take a look
    You would?! Hey thanks Kyle. I would be interested in your feedback. Although you will probably laugh or cry when you see the code I wrote/am using! (I have never learnt HTML. I will be doing a course on it soon) To avoid going off this threads topic, I have opened a new thread here: http://www.excelforum.com/excel-prog...rer-class.html
    Last edited by mc84excel; 07-05-2016 at 08:26 PM.

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

    Re: Which option is faster? Passing a 2D Array

    You haven't posted the scraping code, you've posted a wrapper around a (as you quite rightly state) very slow method of web interaction. To be completely blunt, until you improve your web interaction method (using direct requests rather than proxying through IE) all you are really doing is polishing a turd

    There are hundreds of reasons that websites respond at inconsistent rates, from rate limiting, to server load through to latency issues. Unless you are doing something really, really funky there is simply no possible way that the changes you propose above (over dimming the array vs redim preserve) could have that impact on performance, I'd put my mortgage on the inconsistency of the web site response.

    Again, if you post your actual scraping code (not your library) I'm happy to give you some pointers, but these will be around direct web requests (possibly queueing asynchronous calls depending on how far you want to push performance), not IE automation. There really isn't a performant way of using IE to scrape websites - there's very little point in the type of optimization you are aiming for here when by factors of 100s your code would be improved by learning how to properly scrape websites.

  18. #18
    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: Which option is faster? Passing a 2D Array

    Quote Originally Posted by Kyle123 View Post
    There are hundreds of reasons that websites respond at inconsistent rates, from rate limiting, to server load through to latency issues. Unless you are doing something really, really funky there is simply no possible way that the changes you propose above (over dimming the array vs redim preserve) could have that impact on performance
    I assure you I have improved my scrape performance that much merely by changing the Redim Preserves. This speed improvement owes nothing to inconsistent website rates as I have a large number of measurements going back for over a year for the site I scrape most. The average has been ~25 to 30 RPM for some time now. Changing the Redim Preserves has pushed it to 38-40 RPM (I got this speed back after I reduced the oversized arrays)

    So I stand by your "really really funky" escape clause!

+ 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. Faster option than VLOOKUP? INDEX/MATCH is taking twice as long...
    By mrbusto71 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-12-2015, 09:44 PM
  2. [SOLVED] Passing Array to OFFSET() - array height parameter
    By GeneralDisarray in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 08-05-2014, 10:48 AM
  3. [SOLVED] How to make excel file faster which uses option buttons?
    By hyperaura in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-26-2013, 01:16 PM
  4. [SOLVED] passing Option button result to textbox
    By tigerdel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-29-2012, 02:35 AM
  5. which option faster and why?
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 06-09-2011, 03:59 PM
  6. Lookups via Array - Can We Make this Faster?
    By prescient in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2009, 11:52 AM
  7. How can I make this faster...array?
    By excelnewbie in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2005, 04:19 PM

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