+ Reply to Thread
Results 1 to 19 of 19

Why the code is slow although using arrays

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Why the code is slow although using arrays

    Hello everyone
    I am trying to build a code but make it general .. That's because there are a lot of changes and new columns added and other columns may be changed in place. So I thought of using modVariables to put the columns numbers in variables and to be easier to follow and edit later
    in modVariable I put these lines
    Please Login or Register  to view this content.
    And in ThisWorkbook module I put these lines
    Please Login or Register  to view this content.
    In modUtility I used some UDFs
    Please Login or Register  to view this content.
    Now when trying the following code that SUMs some columns, I found it takes about 5 seconds for only 7000 rows
    Please Login or Register  to view this content.
    The code is simple as it sums columns together and put the results in array
    What makes the code slow...? I like to hear your ideas
    I can't attach sample now but it is so simple and the data are just numbers ..
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Why the code is slow although using arrays


    Hi,

    what takes time is writing data to cells …
    How many time requires the execution to achieve the loop, so before updating data in cells ?

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Why the code is slow although using arrays

    @ Marc

    How many time requires the execution to achieve the loop
    I know the answer to that question.

    7000 rows x 12 jumps back and forth to UDF's per Row = 84000.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Why the code is slow although using arrays

    Thanks a lot for replies. What's the best alternative in such cases ..?
    I guessed using arrays would help but it seems not too much.

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Why the code is slow although using arrays


    So compare the time execution with another code.

    Avoid data type conversion like for example Val, can't you work directly with numbers rather than strings ?
    Last edited by Marc L; 05-17-2020 at 09:31 PM.

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Why the code is slow although using arrays

    Thanks a lot for the advice.. I will try to avoid VAL .. but is such a point may affect in that way?

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Why the code is slow although using arrays


    It's peanut in a normal code but if you are looping x times you multiply x times the data types conversion !
    It's weird to work with arrays but not with well data types as before looping you can create once a data types conversion in an array
    and use it within the loop without the need to convert anything ! In a recent thread needing some 'combinatorics engine'
    I divided the time execution by seven just avoiding the data types conversion in the engine used within a loop …

  8. #8
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Why the code is slow although using arrays

    Can you provide me with the link please to have a look?

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Why the code is slow although using arrays


    As only the more efficient code was posted so you can't compare with the first try.
    It's just a question of obvious logic, the less operations within the loop, the faster …

    Same question than post #2 : how many time execution the loop needs actually, 2-3 seconds ?
    Last edited by Marc L; 05-18-2020 at 08:28 AM.

  10. #10
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Why the code is slow although using arrays

    I don't know exactly how many time execution the loop needs actually!!
    Maybe about two seconds

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Why the code is slow although using arrays


    According to your initial post, if the loop needs only 2 seconds so the data allocation from array to range needs 3 seconds !
    And these 3 seconds can't be reduced or without any code just with Excel formulas if possible …

    So for the process time execution you can try to create a data numeric array in order you do not ever need to use Val
    - or any data type conversion statement - to compare how much time this way may save
    but as the 3 seconds to allocate data to cells still remains …

  12. #12
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Why the code is slow although using arrays

    Thanks a lot. I already was using formulas in the code using the traditional methods and this would take about 15 seconds so I tried to think in a different way..

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Thumbs up Re: Why the code is slow although using arrays


    So you have divided the time by 3, that's nice, isn't it ?

  14. #14
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Why the code is slow although using arrays

    Yes that's nice no doubt. But I am greedy and searching for more speed as I am sure this would be done in less than a second ..

  15. #15
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Why the code is slow although using arrays


    As per posts #2 & 11 how can it be done in less than a second as your array requires 3 seconds to be written to cells ?‼

  16. #16
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Why the code is slow although using arrays

    I will try to do my best. I will not give up easily

  17. #17
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Why the code is slow although using arrays


    Using an array, you can only try to improve the time calculation …

  18. #18
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,844

    Re: Why the code is slow although using arrays

    I did not try to piece together all of your code into something, so I made something from scratch. A spreadsheet with about 7000 rows x 52 columns of random numbers (pasted as values), then ran code like this
    Please Login or Register  to view this content.
    The results of my test on my machine:
    1st time -- read 350000 cells into array=90 ms, copy 12 columns into new array=20 ms, write 50000 cells to spreadsheet=50 ms.
    2nd time -- read 350000 cells into array=90 ms, copy 12 columns with call to max function=700 ms, write 50000 cells to spreadsheet=50 ms.
    3rd time -- read 350000 cells into array=90 ms, sum 12 pairs of columns=40 ms, write 50000 cells to spreadsheet=50 ms.
    4th time -- read 350000 cells into array=90 ms, sum 12 pairs of columns with call to max function=1400 ms, write 50000 cells to spreadsheet=50 ms.

    I obviously did not take the time to test every bottleneck or every other variation, but, from those 4 tests, it appears to me that call Excel's MAX() function adds a significant amount of time to the processing. You can use the Timer function in your own code similar to what I have done here to test for bottlenecks associated with your own project.

    I might also add this observation. I built a spreadsheet only that used SUM(MAX(INDEX(...)),MAX(INDEX(...)),...) functions that performs a similar set of summations. That spreadsheet only approach took about 2 seconds to process 1E5 random records. I know most of your projects are VBA heavy, but, if processing time is important, it might be worth considering non-VBA approaches (worksheet functions in a spreadsheet or pivot tables or similar).
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  19. #19
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Why the code is slow although using arrays

    Thank you very much for this detailed reply. I recognize now that using Application.MAX is the cause and I implemented that in the code using the formulas and tried to implement that into the arrays and this gives me perfect results ..
    Thank you very much for all of you

+ 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. Slow macro, slow pc or wrong code?
    By corky81 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-24-2018, 05:58 AM
  2. [SOLVED] Very inefficient code because of different sizes of arrays, how do i shorten my code?
    By Brammer88 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-28-2012, 04:49 PM
  3. Tons of Arrays = Super Slow Calculating
    By danmarsh in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-23-2012, 01:52 PM
  4. slow vba loop with arrays
    By danmcdon in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-16-2012, 10:55 PM
  5. Replies: 5
    Last Post: 06-03-2010, 01:04 PM
  6. slow for loops ... better way using arrays or something?
    By wdeleo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-28-2006, 10:35 AM
  7. Formula Arrays VERY SLOW in Excel 2002
    By Patrick in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-26-2005, 09:06 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