CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2013
    Posts
    25

    nested loops to streamlined code

    all,

    in C++ right now, I have code similar to this (symantics may not be right, just ignore it. Example came from PHP):

    Code:
            If (ctr = 4) {
              for (block = 1; block <= 27; block += 3) {
                for (ctr2 = 1; ctr2 <= 3; ctr2++) { //each line.
                  for (ctr3 = 1; ctr3 <= 3; ctr3++) { //char 1 - 3 in each line.
                    switch (ctr2) { //which line?
                      Case 1:
                        strOut = strOut . substr(Line1, block - 1 + ctr3, 1);
                      Case 2:
                        strOut = strOut . substr(Line2, block - 1 + ctr3, 1);
                      Case 3:
                        strOut = strOut . substr(Line3, block - 1 + ctr3, 1);
                    }
                  }
                }
              }
            }
    I don't have a spreadsheet or anything set up to where I can identify all internal resources that C++ has to streamline this, either by mathematical means or method means.

    can someone help me here in that regard? anyone know how can this be consolidated using C++ resources?

    I never like writing long iterations, but as it just so happens, this can be done very rapidly without having to re-learn any given language with the purpose of using cleaner/better coding techniques.

    any help appreciated. thanks.

  2. #2
    Join Date
    Feb 2013
    Location
    United States
    Posts
    56

    Lightbulb Re: nested loops to streamlined code

    You could take the switch statement outside of the third loop. That should speed it up a tiny bit. Also, since loops two and three only get iterated three times, you could unroll them to get better performance.

    Code:
    	if (ctr == 4)
    	{
    		for (block = 1; block <= 27; block += 3)
    		{
    			strOut = strOut . substr(Line1, block, 1);
    			strOut = strOut . substr(Line1, block + 1, 1);
    			strOut = strOut . substr(Line1, block + 2, 1);
    			strOut = strOut . substr(Line2, block, 1);
    			strOut = strOut . substr(Line2, block + 1, 1);
    			strOut = strOut . substr(Line2, block + 2, 1);
    			strOut = strOut . substr(Line3, block, 1);
    			strOut = strOut . substr(Line3, block + 1, 1);
    			strOut = strOut . substr(Line3, block + 2, 1);
    		}
    	}
    From further analysis this could be simplified to
    Code:
    	if (ctr == 4)
    	{
    		for (block = 1; block <= 27; block += 3)
    		{
    			strOut = strOut . substr(Line1, block, 3);
    			strOut = strOut . substr(Line2, block, 3);
    			strOut = strOut . substr(Line3, block, 3);
    		}
    	}
    You could also eliminate the multiple calls to the string concatenate function, which may be reallocating memory each time, by using a fixed array of 81 characters, filling that with the substrings, and then concatenating that to the output string.
    Last edited by Coder Dave; March 6th, 2013 at 10:14 PM.

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