+ Reply to Thread
Results 1 to 16 of 16

Running code hangs Excel for more than an hour

  1. #1
    Forum Expert tim201110's Avatar
    Join Date
    10-23-2011
    Location
    Russia
    MS-Off Ver
    2016, 2019
    Posts
    2,357

    Running code hangs Excel for more than an hour

    Hi,
    I need this code for some check of Gaussian distribution.
    Please Login or Register  to view this content.
    Computer I7, 8GB, 1,36GHz, SSD, Excel 2013
    Excel takes not more than 30% of RAM and 20% processor time
    What is wrong with it?

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Running code hangs Excel for more than an hour

    Hi tim,

    I'm not surprised... you are asking excel to loop for a trillion iterations, generating a random number each time... that's going to take some time
    Aditionally it is holding a significant array in memory while the code runs.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,888

    Re: Running code hangs Excel for more than an hour

    What's "j" variable for?

    You are only using "i" variable in the code...

    May be try this?
    Please Login or Register  to view this content.
    Took less than 1 sec on my machine. i5, 2.5GHz, 16Gb RAM.

  4. #4
    Forum Expert tim201110's Avatar
    Join Date
    10-23-2011
    Location
    Russia
    MS-Off Ver
    2016, 2019
    Posts
    2,357

    Re: Running code hangs Excel for more than an hour

    Quote Originally Posted by Arkadi View Post
    Hi tim,

    I'm not surprised... you are asking excel to loop for a trillion iterations, generating a random number each time... that's going to take some time
    Aditionally it is holding a significant array in memory while the code runs.
    Arkadi, thanks for reply.
    Is there a way to roughly estimate running time?

  5. #5
    Forum Expert tim201110's Avatar
    Join Date
    10-23-2011
    Location
    Russia
    MS-Off Ver
    2016, 2019
    Posts
    2,357

    Re: Running code hangs Excel for more than an hour

    Quote Originally Posted by CK76 View Post
    What's "j" variable for?

    You are only using "i" variable in the code...
    CK76,
    function Rnd is absolutly random.
    if one sums some Rnds, he will have a Gaussian distribution generator.

  6. #6
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Running code hangs Excel for more than an hour

    @CK I assume he wants to add 1 million random numbers together to store in the 1 million array items?
    @tim not really no, you could try doing this for 1,000 i and j loops.... time it, and then multiply that time by 1,000,000.. that may give you a rough estimate

    EDIT! I corrected the post, I said multiply by 1,000 i meant 1,000,000
    Last edited by Arkadi; 01-20-2017 at 03:03 PM.

  7. #7
    Forum Expert tim201110's Avatar
    Join Date
    10-23-2011
    Location
    Russia
    MS-Off Ver
    2016, 2019
    Posts
    2,357

    Re: Running code hangs Excel for more than an hour

    Thanks.
    i will go to bed, it will calculate

  8. #8
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Running code hangs Excel for more than an hour

    tim, I don't see it really saving any time, but if your second array dimension is only 1 to 1 then you don't really need to make it a 2d array? Seems to me you could just use:

    Please Login or Register  to view this content.

  9. #9
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Running code hangs Excel for more than an hour

    You may also want to actually initialize the random number function:

    Please Login or Register  to view this content.
    without randomize, the "random" numbers will be the same if you run the code twice

  10. #10
    Forum Expert tim201110's Avatar
    Join Date
    10-23-2011
    Location
    Russia
    MS-Off Ver
    2016, 2019
    Posts
    2,357

    Re: Running code hangs Excel for more than an hour

    I do not think it will save a lot of memory.
    besides, there are ussually two dimentions for working with range.

  11. #11
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,888

    Re: Running code hangs Excel for more than an hour

    I assume he wants to add 1 million random numbers together to store in the 1 million array items?
    Ah, got it. I missed the part about Gaussian distribution.

    Yeah, it's going to take a long time no matter how you code this one. And unfortunately, VBA can't use multi-threading to my knowledge.

    1,000,000 iteration for each array element. To populate 1,000,000 array element takes about 0.73 seconds.
    So... 730,000 seconds to calculate in best circumstance... 202 hours and 47 min.

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

    Re: Running code hangs Excel for more than an hour

    if one sums some Rnds, he will have a Gaussian distribution generator.
    Is this the goal of this code/part of the project? This is way out of my area of expertise, but I wonder if there is a better, faster algorithm for generating such random numbers.

    A quick internet search found this at Wikipedia for the Box-Muller transform: https://en.wikipedia.org/wiki/Box%E2...ller_transform
    and this one: http://www.design.caltech.edu/erik/Misc/Gaussian.html

    This algorithm seemed simple enough, so I tried it:
    A1=RAND()
    B1=RAND()
    C1=SQRT(-2*LN(A1))
    D1=2*PI()*B1
    E1=C1*COS(D1)
    F1=C1*SIN(D1)
    copied down to the bottom (row 1048576). This results in two sets of random Gaussian numbers (mean~0 stdev~1). After building the spreadsheet, it calculates in about 1 second (i3 3.2 GHz). I also noted from the Box-Muller Wikipedia page that Box-Muller is considered a "slower" algorithm. Wikipedia mentions the Ziggurat algorithm that is supposedly even faster.

    That is probably not the final solution to your problem. I would suggest that, if computation time is a real concern, that you do some research into other random number generation algorithms to find a faster algorithm than this "brute force" algorithm you are currently using.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  13. #13
    Forum Expert tim201110's Avatar
    Join Date
    10-23-2011
    Location
    Russia
    MS-Off Ver
    2016, 2019
    Posts
    2,357

    Re: Running code hangs Excel for more than an hour

    column A just Rnd, B with Randomize
    there is no difference
    file is too big for the forum
    code:
    Please Login or Register  to view this content.
    Attached Images Attached Images

  14. #14
    Forum Expert tim201110's Avatar
    Join Date
    10-23-2011
    Location
    Russia
    MS-Off Ver
    2016, 2019
    Posts
    2,357

    Re: Running code hangs Excel for more than an hour

    MrShorty, thanks
    I know about it
    i used this (not satisfied):
    Attached Images Attached Images

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

    Re: Running code hangs Excel for more than an hour

    As noted, this is far from my area of expertise, so I cannot comment in more detail. I was hoping that I had found some ideas for researching faster algorithms, but it appears that you have already researched these algorithms and found them unsuitable.

    I don't see any way to speed up this brute force algorithm, so if it is the only algorithm that will work for you, it may be necessary to wait it out.

  16. #16
    Forum Expert tim201110's Avatar
    Join Date
    10-23-2011
    Location
    Russia
    MS-Off Ver
    2016, 2019
    Posts
    2,357

    Re: Running code hangs Excel for more than an hour

    thank all of you!
    1000000 was a greed, i will try 1000

+ 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. code works running in visual basic editor but not when running from Excel macro
    By smporco in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-06-2016, 05:44 PM
  2. [SOLVED] Excel VB code. Message pops up while code running asking question. Code must not wait.
    By Heinrich Venter in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-28-2014, 06:10 AM
  3. [SOLVED] Excel Hangs when running Macro unless resource monitor open on top of excel??????
    By JimBobBowie in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-16-2014, 10:55 AM
  4. What to do when code hangs..
    By lsmcal1984 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-20-2013, 11:11 AM
  5. VBA Code hangs at different points
    By kbbraman in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 03-13-2013, 12:11 PM
  6. Running a code at a specific hour wont work if...
    By Joaoack in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-14-2012, 03:23 PM
  7. [SOLVED] Code hangs Excel ... Why? ... and how can i debug??
    By Simon May in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-17-2005, 03:06 AM

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