CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Nov 2009
    Posts
    27

    how much does array size affect compiling speed?

    so my current program uses two array variables, each of size 250x250, and I use the ofstream function to output the stuff on Notepad. When I first ran it with each array variable of size 50x50, I didn't have any problems and it didnt take too long to run. But when I increased the size of each of them to 250x250, I'm having all sorts of problems. When I ran the simulation (each simulation runs the program) too many times, the program crashed. I then tried eliminating one of the variables, and while the program runs, it takes ridiculously long to run. It would take 23 hours! to do the number of simulations I want, whereas I think it only took 10 minutes for the 50x50 case.

    I attached the relevant part of the program in this message. The parts including:

    if ((x_value)<0.25)
    {x--;
    LeftCount++;


    For the left, right, down, and up cases are negligible since they don't affect the 'island[][]' array variable
    So what can I possibly do to speed up the program? Do I have no choice but to use a smaller array size?

  2. #2
    Join Date
    Nov 2009
    Posts
    27

    Re: how much does array size affect compiling speed?

    The program is in this message
    Attached Files Attached Files

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: how much does array size affect compiling speed?

    it depends what it does...
    Increasing the size of an array itself won't make much of a difference. but the amount of work done can increase dramatically.
    If you have an N*M type problem, then increasing from 50x50 to 250x250 will mean 25times more work to be done, so you can expect at least a 25fold increase in time to execute.

    if your problem is exponential in nature, it could be a lot worse.

    But it gets worse... while 50x50 is fairly small it means you can probably still fit all of the data into the L2 CPU cache (maybe even the L1 cache), while with 250x250 this will no longer be the case.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: how much does array size affect compiling speed?

    1. Your code cannot compile
    2. You didn't show declarations/definitions of a lot of the variables
    3. .txt file is not the best to post code snippets: you should either attach .cpp, .h files (or a whole project in zip archive) or post code snippets directly using Code tags around them
    4. What do all these magic numbers (0.004, .5, 0.25, 0.75, ..., 10, 125, 124, ..., 116, ...) mean?
    Victor Nijegorodov

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: how much does array size affect compiling speed?

    5. What does your problem have to do with compiling speed?

  6. #6
    Join Date
    Nov 2009
    Posts
    27

    Re: how much does array size affect compiling speed?

    Quote Originally Posted by OReubens View Post
    it depends what it does...
    Increasing the size of an array itself won't make much of a difference. but the amount of work done can increase dramatically.
    If you have an N*M type problem, then increasing from 50x50 to 250x250 will mean 25times more work to be done, so you can expect at least a 25fold increase in time to execute.

    if your problem is exponential in nature, it could be a lot worse.

    But it gets worse... while 50x50 is fairly small it means you can probably still fit all of the data into the L2 CPU cache (maybe even the L1 cache), while with 250x250 this will no longer be the case.
    whats the L2 CPU cache? The last time i took a c++ class was 2.5 years ago, and I don't think I learned about that.

    Quote Originally Posted by VictorN View Post
    1. Your code cannot compile
    2. You didn't show declarations/definitions of a lot of the variables
    3. .txt file is not the best to post code snippets: you should either attach .cpp, .h files (or a whole project in zip archive) or post code snippets directly using Code tags around them
    4. What do all these magic numbers (0.004, .5, 0.25, 0.75, ..., 10, 125, 124, ..., 116, ...) mean?
    the code does compile. It just crashes if I have two 250x250 array variables. I didn't list the entire code in that message, since I don't think you guys want to see all of it, but I'll attach it in this message anyways

    .004 is 1/250 and .5, .25, .75, 1 are the the ranges used to assign the random movements based on the random number generator. They don't affect the array.

    Quote Originally Posted by GCDEF View Post
    5. What does your problem have to do with compiling speed?
    Thats what I want to know? I don't know much about arrays



    I've noticed that the program runs significantly slower as the value of j, which is related to n_simulations, increases. It runs fast, as expected, for the first 10 values or so for j, then runs ALOT slower as j gets in the range of 50, but it may get slower before that, as i havent checked the speed for j between those values
    Attached Files Attached Files
    Last edited by larry burns; November 20th, 2009 at 01:44 PM.

  7. #7
    Join Date
    Nov 2009
    Posts
    27

    Re: how much does array size affect compiling speed?

    that previous attachment is wrong. the correct one is in this message. I fixed one of the if statements, as the previous one evaluated a few of them as 'if island[][250]', but island only goes from 0 to 249. But even with the corrections, it runs only a little faster, and would still take about 16 hours to have 11,250 n_simulations, as my professor wants
    Attached Files Attached Files
    Last edited by larry burns; November 20th, 2009 at 01:48 PM.

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: how much does array size affect compiling speed?

    Your code formatting is very… hmmm… artistic?
    None of the formatting styles I saw places an open bracket on the same line as the next statement.
    I see how you can make an exception for a construct that fits nicely on one line (for compactness).
    However, this makes NO SENSE at all:
    Code:
    {int t=1;}
    You define a local variable t that goes out of scope right away. (This happens more than once in your code).
    Here is another senseless line:
    Code:
    void close();
    It declares a void function close(), which is not defined in your code, but that’s OK – it is never called either

    Now one thing about efficiency. You have this nested loop (a few times):
    Code:
    for (int a=0; a<250; a++)
    {    
    	for (int b=0; b<250; b++)
    	{ 
    		if (island[125-y][x+124]>island[a][b])
    		{t=0;}
    
    	}
    }
    Note that inner part is executed 62,500 times.
    Note also that after the very first time you set t to 0, the rest of those loops can’t possibly make any difference. See if you can break out of there.

    This whole thing is triple-nested into
    Code:
    for(int i=0;i<n_walks;i++)// 'FOR' STATEMENT IS A TYPE OF LOOP
    {
    	for (int f=125; f>-116; f=f-10)
    	{
    		for (int g=125; g>-116; g=g-10)
    		{
    which produces 500,000 * 25 * 25 = 312,500,000 iterations of the previous fragment. No wonder it takes long time!

    Could you please clean up you code and repost here for further analysis?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  9. #9
    Join Date
    Nov 2009
    Posts
    27

    Re: how much does array size affect compiling speed?

    Quote Originally Posted by VladimirF View Post
    However, this makes NO SENSE at all:
    Code:
    {int t=1;}
    You define a local variable t that goes out of scope right away. (This happens more than once in your code).



    Now one thing about efficiency. You have this nested loop (a few times):
    Code:
    for (int a=0; a<250; a++)
    {    
    	for (int b=0; b<250; b++)
    	{ 
    		if (island[125-y][x+124]>island[a][b])
    		{t=0;}
    
    	}
    }
    Note that inner part is executed 62,500 times.
    Note also that after the very first time you set t to 0, the rest of those loops can’t possibly make any difference. See if you can break out of there.

    This whole thing is triple-nested into
    Code:
    for(int i=0;i<n_walks;i++)// 'FOR' STATEMENT IS A TYPE OF LOOP
    {
    	for (int f=125; f>-116; f=f-10)
    	{
    		for (int g=125; g>-116; g=g-10)
    		{
    which produces 500,000 * 25 * 25 = 312,500,000 iterations of the previous fragment. No wonder it takes long time!

    Could you please clean up you code and repost here for further analysis?
    what do you mean the t=1 goes out of scope right away? I need it there so it can run through the if(t!=0) part

    Thanks alot for that suggestion to break out of the loop after setting t to 0. My program now runs faster, but its still ridiculously slow. It would now take 23 hours or so to run 11,250 n_simulations

    how can I clean up the code? Sorry for the mess, but isn't it easier to read if I skip a line for the 'for' statements?
    Attached Files Attached Files

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how much does array size affect compiling speed?

    Quote Originally Posted by larry burns View Post
    what do you mean the t=1 goes out of scope right away? I need it there so it can run through the if(t!=0) part
    C++ has rules:
    Code:
    {
       int t=1;
    }
    You declared an integer inside that block { } and called it "t". As soon as that block is exited (the } is reached), that "t" no longer exists. This is called "variable lifetime" or "scope".

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Nov 2009
    Posts
    27

    Re: how much does array size affect compiling speed?

    Quote Originally Posted by Paul McKenzie View Post
    C++ has rules:
    Code:
    {
       int t=1;
    }
    You declared an integer inside that block { } and called it "t". As soon as that block is exited (the } is reached), that "t" no longer exists. This is called "variable lifetime" or "scope".

    Regards,

    Paul McKenzie
    I made those changes. Also, I noticed I opened the ofstream too much, and that I'm better off opening the file once, writing everything, then closing after all the data is written.

    With all those changes, the program runs slightly faster, but would still take 20 hours to run. Theres gotta be other problems

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how much does array size affect compiling speed?

    Quote Originally Posted by larry burns View Post
    I made those changes. Also, I noticed I opened the ofstream too much, and that I'm better off opening the file once, writing everything, then closing after all the data is written.

    With all those changes, the program runs slightly faster, but would still take 20 hours to run. Theres gotta be other problems
    The "problem" is that you have nested loops inside of nested loops inside of nested loops, etc.. Multiply those iterations, and you have to be in the millions of iterations occurring. What were you expecting?

    Secondly, I have to agree with others about your formatting -- your code is very hard to read. For the sake of the others here, I've attached is a more coherently formatted version of your code, which you should be using.

    Third, the main() function returns int, not void.

    And lastly, you don't need a loop to initialize those arrays to 0. See the updated source code I've attached. Also, please take what I posted, and change it to what you have now. In other words, keep the formatting that I have and change the program to your current version without the "t" mistakes you had.

    Regards,

    Paul McKenzie
    Attached Files Attached Files
    Last edited by Paul McKenzie; November 21st, 2009 at 02:09 AM.

  13. #13
    Join Date
    Nov 2009
    Posts
    27

    Re: how much does array size affect compiling speed?

    Quote Originally Posted by Paul McKenzie View Post
    The "problem" is that you have nested loops inside of nested loops inside of nested loops, etc.. Multiply those iterations, and you have to be in the millions of iterations occurring. What were you expecting?

    Secondly, I have to agree with others about your formatting -- your code is very hard to read. For the sake of the others here, I've attached is a more coherently formatted version of your code, which you should be using.

    Third, the main() function returns int, not void.

    And lastly, you don't need a loop to initialize those arrays to 0. See the updated source code I've attached. Also, please take what I posted, and change it to what you have now. In other words, keep the formatting that I have and change the program to your current version without the "t" mistakes you had.

    Regards,

    Paul McKenzie
    thanks alot for the improved formatting version of my program. Anyways, is there anything else I can do to improve my nested loops ?

  14. #14
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: how much does array size affect compiling speed?

    Quote Originally Posted by larry burns View Post
    Anyways, is there anything else I can do to improve my nested loops ?
    You could post your most current code…
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  15. #15
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: how much does array size affect compiling speed?

    One more comment. This code fragment:
    Code:
    int t = 1;
    for ( int a = 0; a < 250; a++ )
    {
        for ( int b = 0; b < 250; b++ )
        {
            if( island[125 - y][x + 124] > island[a][b] )
            {
                t = 0;
                b = 1000;
                a = 1000;
            }
        }
    }
    made me think for a second: why 1000? Then I realized that this is how you are breaking from the loop.
    There is a MUCH better way to break out of nested loop (well, even a goto would be better that this):
    Code:
    int t = 1;
    for ( int a = 0; a < 250 && t == 1; a++ )
    {
        for ( int b = 0; b < 250 && t == 1; b++ )
        {
            if( island[125 - y][x + 124] > island[a][b] )
            {
                t = 0;
            }
        }
    }
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured