I was told that despite the min and max functions being called in the declaration of the loop, they get called for each iteration of the loop and that therefore I should store them in variables and use those variables. Is this true?


Code:
if (vTmp1.x == vTmp2.x) {
            for (int y = min(vTmp1.y, vTmp2.y); y <= max(vTmp1.y, vTmp2.y); ++y) {
                putPixel(vTmp1.x, y, fillColor);
            }
        } else if (vTmp1.y == vTmp2.y) {
            for (int x = min(vTmp1.x, vTmp2.x); x <= max(vTmp1.x, vTmp2.x); ++x) {
                putPixel(x, vTmp1.y, fillColor);
            }
        }


vs.


Code:
if (vTmp1.x == vTmp2.x) {
            int yMax = max(vTmp1.y, vTmp2.y);
            int yMin = min(vTmp1.y, vTmp2.y);
            for (int y = yMin; y <= yMax; ++y) {
                putPixel(vTmp1.x, y, fillColor);
            }
        } else if (vTmp1.y == vTmp2.y) {
            int xMin = min(vTmp1.x, vTmp2.x);
            int xMax = max(vTmp1.x, vTmp2.x);
            for (int x = xMin; x <= xMax; ++x) {
                putPixel(x, vTmp1.y, fillColor);
            }
        }
Only if all the compiler can see is an unattributed declaration for min and max, in which case it doesn't actually have a choice since an arbitrary function call could do anything, including returning a different value every time it is called or having arbitrary side effects. As you can see here, only test1 calls max inside the loop, the rest do not call either max or min in the loop.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement