Thread: For Loops: Which Method Is Better?

  1. #16
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    Depends on how the loop looks like, and the type of hardware.
    A modern OOO CPU will likely just continue to loop while stuffing down ops through the pipeline, so it essentially "unrolls the loop" itself.
    If there are too many data dependencies or too many loops (as in your example), then you'll have problems, though.
    I'm just speaking from a theoretical perspective, though. I can't say from a practical one, as I am not well versed in compiler technologies.
    Right, my example isn't saved by loop unrolling. Each iteration through the first loop depends on the previous iteration, so the CPU will have to wait for each product computation before going on to the next. That makes for bad throughput and low resource utilization (of ALUs). But if the two loops are merged, then the CPU can, via pipelining, compute the sum for the second loop at the same time as it computes the product for the first.

    Quote Originally Posted by CornedBee View Post
    Screw performance. Do the thing that makes the code readable. You optimize only when the program isn't fast enough and profiling has shown you where the slow parts are.

    In King Mir's example, you have one function doing two very different things (getting the product of an array and getting elementwise sums of two arrays), so using two loops, and two separate functions in fact, is better. And while you're at it, get rid of those loops entirely and use algorithms.

    Code:
    int product(int array[], int size) {
      return std::accumulate(array, array+size, 1, std::multiplies<int>());
    }
    
    void sum_elements(int input1[], int input2[], int output[], int size) {
      std::transform(input1, input1+size, input2, output, std::plus<int>());
    }
    It's a contrived example to be sure. And I agree that you only optimize when you've established a need for it. But it is better performance to use one loop.
    Last edited by King Mir; 06-19-2012 at 07:23 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Still, in that case I'd prefer to use compiler-specific noalias annotations and have the compiler fuse the loops. Yes, it's not as reliable, but it doesn't destroy the logical code structure.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Whether to split/fuse a loop is more than an optimization decision. It clearly can be, but it also communicates intent.

    For instance suppose you have three 100-entry arrays which are used for completely different purposes. The fact that they are each 100 elements long is merely coincidence. In this case you would NOT fuse the initialization of these arrays into a single loop, because the fact that they are all 100 elements long is happenstance. Not only is the code brittle to changes in the size of the arrays, it also mistakenly communicates to the reader that there is a relationship between the arrays that doesn't really exist. Writing the initializations as three loops is appropriate.

    Also, when optimizing it is not necessarily better to unroll a loop -- rather to do the opposite by splitting operations on distinct memory objects into separate loops to utilize the cache more efficiently. So one could argue that both splitting a loop and fusing a loop are optimizations, depending on the circumstances.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #19
    Registered User
    Join Date
    May 2012
    Posts
    1
    Both are different.
    first one produces: abc
    abc
    abc
    ...
    second one produces : aaa
    bbb
    ccc
    ...
    for c++ refer C++ Basics

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    sumtaru: you might want to read the thread a little before responding. You're three days late.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting for loops to while loops
    By nabhatt in forum C Programming
    Replies: 3
    Last Post: 02-16-2012, 09:45 PM
  2. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  3. loops, menu loops
    By gloworm in forum C Programming
    Replies: 17
    Last Post: 04-12-2010, 07:59 PM
  4. difference between this->method() and method()
    By nacho4d in forum C++ Programming
    Replies: 7
    Last Post: 11-21-2009, 04:11 PM
  5. Encrypt method (from decrypt method)
    By mmmmmm in forum C# Programming
    Replies: 3
    Last Post: 09-19-2009, 10:35 AM