Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Good way to optimize or replace a nested for loop?

Good way to optimize or replace a nested for loop?

Scheduled Pinned Locked Moved C / C++ / MFC
code-reviewc++performancetutorialquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NilssonCreative
    wrote on last edited by
    #1

    Hi. A friend of mine wrote this crazy function in VB and i have translated it to C++ in hopes that we could get a bit better performance. I guess the performance might be a bit better but it is still an enourmous amount of loops to go through. Do you have any suggestions on how to optimize this loop or perhaps replace it completely to be able to finish all iterations in a managable time? Is it hard to have it run on the GPU and would that improve anything? // Parre

    int CountTheOffsetValids() {

    int c0;
    int c1;
    int c2;
    int c3;
    int c4;
    int c5;
    int c6;
    int c7;
    int c8;
    int c9;
    int c10;
    int c11;
    int c12;
    int c13;
    int c14;
    int c15;
    int c16;
    int c17;
    int c18;
    int c19;
    
    int llim \[20\] = {1, 2, 5, 8, 11, 14, 17, 21, 24, 28, 31, 35, 38, 42, 45, 50, 53, 58, 61, 66};
    int hlim \[20\] = {6, 10, 14, 19, 22, 26, 29, 34, 37, 41, 44, 48, 51, 54, 57, 61, 63, 67, 69, 70};
    
    
    for (c0 = llim\[0\]; c0 <= hlim\[0\]; ++c0) {
            arrVals\[0\] = c0;
    		for (c1 = llim\[0\]; c1 <= hlim\[1\]; ++c1) {
                arrVals\[1\] = c1;
                if (c1 > c0) {
                    for (c2 = llim\[2\]; c2 <= hlim\[2\]; ++c2) {
                        arrVals\[2\] = c2;
                        if (c2 > c1) {
                            for (c3 = llim\[3\]; c3 <= hlim\[3\]; ++c3) {
                                arrVals\[3\] = c3;
                                if (c3 > c2) {
                                    for (c4 = llim\[4\]; c4 <= hlim\[4\]; ++c4) {
                                        arrVals\[4\] = c4;
                                        if (c4 > c3) {
                                            for (c5 = llim\[5\]; c5 <= hlim\[5\]; ++c5) {
                                                arrVals\[5\] = c5;
                                                if (c5 > c4) {
                                                    for (c6 = llim\[6\]; c6 <= hlim\[6\]; ++c6) {
                                                        arrVals\[6\] = c6;
                                                        if (c6 > c5) {
                                                            for (c7 = llim\[7\]; c7 <= hlim\[7\]; c7) {
                                                                arrVals\[7\] = c7;
                                                                if (c7 > c6) {
                                                                    for (c8 = llim\[8\]; c8 &lt
    
    _ 1 Reply Last reply
    0
    • N NilssonCreative

      Hi. A friend of mine wrote this crazy function in VB and i have translated it to C++ in hopes that we could get a bit better performance. I guess the performance might be a bit better but it is still an enourmous amount of loops to go through. Do you have any suggestions on how to optimize this loop or perhaps replace it completely to be able to finish all iterations in a managable time? Is it hard to have it run on the GPU and would that improve anything? // Parre

      int CountTheOffsetValids() {

      int c0;
      int c1;
      int c2;
      int c3;
      int c4;
      int c5;
      int c6;
      int c7;
      int c8;
      int c9;
      int c10;
      int c11;
      int c12;
      int c13;
      int c14;
      int c15;
      int c16;
      int c17;
      int c18;
      int c19;
      
      int llim \[20\] = {1, 2, 5, 8, 11, 14, 17, 21, 24, 28, 31, 35, 38, 42, 45, 50, 53, 58, 61, 66};
      int hlim \[20\] = {6, 10, 14, 19, 22, 26, 29, 34, 37, 41, 44, 48, 51, 54, 57, 61, 63, 67, 69, 70};
      
      
      for (c0 = llim\[0\]; c0 <= hlim\[0\]; ++c0) {
              arrVals\[0\] = c0;
      		for (c1 = llim\[0\]; c1 <= hlim\[1\]; ++c1) {
                  arrVals\[1\] = c1;
                  if (c1 > c0) {
                      for (c2 = llim\[2\]; c2 <= hlim\[2\]; ++c2) {
                          arrVals\[2\] = c2;
                          if (c2 > c1) {
                              for (c3 = llim\[3\]; c3 <= hlim\[3\]; ++c3) {
                                  arrVals\[3\] = c3;
                                  if (c3 > c2) {
                                      for (c4 = llim\[4\]; c4 <= hlim\[4\]; ++c4) {
                                          arrVals\[4\] = c4;
                                          if (c4 > c3) {
                                              for (c5 = llim\[5\]; c5 <= hlim\[5\]; ++c5) {
                                                  arrVals\[5\] = c5;
                                                  if (c5 > c4) {
                                                      for (c6 = llim\[6\]; c6 <= hlim\[6\]; ++c6) {
                                                          arrVals\[6\] = c6;
                                                          if (c6 > c5) {
                                                              for (c7 = llim\[7\]; c7 <= hlim\[7\]; c7) {
                                                                  arrVals\[7\] = c7;
                                                                  if (c7 > c6) {
                                                                      for (c8 = llim\[8\]; c8 &lt
      
      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      The C++ standard template library (STL) comes with a lot of algorithms optimized for performance. So if you can tell me what the above code is trying to do, maybe I could suggest an STL algorithm that does the same. Not sure if the GPU will be able to do this, because I see a lot of data dependencies between the loop variables. AFAIK, GPU programming needs the loop variables to be independent of each other.

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++) (October 2009 - September 2013)

      Polymorphism in C

      N 1 Reply Last reply
      0
      • _ _Superman_

        The C++ standard template library (STL) comes with a lot of algorithms optimized for performance. So if you can tell me what the above code is trying to do, maybe I could suggest an STL algorithm that does the same. Not sure if the GPU will be able to do this, because I see a lot of data dependencies between the loop variables. AFAIK, GPU programming needs the loop variables to be independent of each other.

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++) (October 2009 - September 2013)

        Polymorphism in C

        N Offline
        N Offline
        NilssonCreative
        wrote on last edited by
        #3

        Hi and thanks for the answer. The code above is trying every combination of values 1-70 and is supposed to output them to an array of 20 values. Example output each iteration: arrVals[] = {3, 6, 12, 15, 22, 26, 29, 34, 37, 41, 44, 48, 51, 54, 57, 61, 63, 67, 69, 70}; Each position in the array has limitations to how big or small they can be as follows: int llim [20] = {1, 2, 5, 8, 11, 14, 17, 21, 24, 28, 31, 35, 38, 42, 45, 50, 53, 58, 61, 66}; int hlim [20] = {6, 10, 14, 19, 22, 26, 29, 34, 37, 41, 44, 48, 51, 54, 57, 61, 63, 67, 69, 70}; The array arrVals is sent to another function each iteration to decide if the current row of 20 is usable or not. I hope this is clear enough, please ask if not.

        _ 1 Reply Last reply
        0
        • N NilssonCreative

          Hi and thanks for the answer. The code above is trying every combination of values 1-70 and is supposed to output them to an array of 20 values. Example output each iteration: arrVals[] = {3, 6, 12, 15, 22, 26, 29, 34, 37, 41, 44, 48, 51, 54, 57, 61, 63, 67, 69, 70}; Each position in the array has limitations to how big or small they can be as follows: int llim [20] = {1, 2, 5, 8, 11, 14, 17, 21, 24, 28, 31, 35, 38, 42, 45, 50, 53, 58, 61, 66}; int hlim [20] = {6, 10, 14, 19, 22, 26, 29, 34, 37, 41, 44, 48, 51, 54, 57, 61, 63, 67, 69, 70}; The array arrVals is sent to another function each iteration to decide if the current row of 20 is usable or not. I hope this is clear enough, please ask if not.

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          Not exactly sure what the condition logic is. But I believe you could use the std::copy_if[^] alogirthm. Other algorithms that could be possible candidates are - std::next_permutation[^] std::generate[^] std::transform[^] std::for_each[^]

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++) (October 2009 - September 2013)

          Polymorphism in C

          N 1 Reply Last reply
          0
          • _ _Superman_

            Not exactly sure what the condition logic is. But I believe you could use the std::copy_if[^] alogirthm. Other algorithms that could be possible candidates are - std::next_permutation[^] std::generate[^] std::transform[^] std::for_each[^]

            «_Superman_»  _I love work. It gives me something to do between weekends.

            _Microsoft MVP (Visual C++) (October 2009 - September 2013)

            Polymorphism in C

            N Offline
            N Offline
            NilssonCreative
            wrote on last edited by
            #5

            I think the std::next_permutation would be perfect tough i need conditions for it so that it doesn't try to find every permutation for all 70^70, which would take forever. I'm actually not sure if it will be fast enough even with conditions. I'm obviously not good enough with C++ to manage this :p

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups