0

According to this blog, this code

for( i =0; i<10; i++)

is slower than this code

for(i=10; i--; )

because

It is faster to test if something is equal to zero than to compare two different numbers.

Is this true? Why?

Max
  • 21,123
  • 5
  • 49
  • 71
  • 1
    How did you time this? Did you turn on optimizations? – NathanOliver May 09 '19 at 17:36
  • 4
    *Apparently this line of code is slower* -- No, it is not apparent. – PaulMcKenzie May 09 '19 at 17:37
  • @NathanOliver It was mentioned on the site. I added the link in the description – BrogrammerDude May 09 '19 at 17:38
  • 1
    Not all sites are correct. Also some of these types of optimization were true in the 1980s but not true today. Compilers no longer require these tricks. – drescherjm May 09 '19 at 17:39
  • 1
    @BrogrammerDude Any modern C++ worth their salt will optimize both loops to the same code. Honestly, would you think that today's compiler writers won't know about both loop constructs, and thus make sure that they are optimized? Compilers these days can do amazing things when it comes to optimizations, something the site you linked to seems to be not aware of. – PaulMcKenzie May 09 '19 at 17:40
  • 7
    Don't use that site. The person is using `#define`'s for constants and believes `for(int* ptrInt = nArray; ptrInt< nArray+n; ptrInt++) *ptrInt=nSomeValue;` is faster than `for(int i=0; i – NathanOliver May 09 '19 at 17:43
  • Their swap "optimization" is also garbage. The standard swap is faster. – NathanOliver May 09 '19 at 17:45
  • 2
    Agreed that that site looks crappy, but I edited the question to call out the specific claim about for loops. It is a claim I have seen made before, so it is worth debunking if it is false. – Max May 09 '19 at 17:46
  • @NathanOliver is there a more trustworthy site for learning how to optimize ur code better? – BrogrammerDude May 09 '19 at 17:46
  • 3
    It's better to only optimize when your profiler tells you to do so. – drescherjm May 09 '19 at 17:47
  • @BrogrammerDude The only thing the site mentions that has merit is to choose a good algorithm. If your algorithm is garbage, trying to tweak a `for` loop by moving the increment around isn't going to help. – PaulMcKenzie May 09 '19 at 17:47
  • 3
    We have a list of [good C++ books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) that covers some of it. There is also a bunch of videos on youtube from the CPPCON channel that has great talks about it. Chandler Carruth has a few talks there over the years that are really good. – NathanOliver May 09 '19 at 17:48
  • 1
    Depends on the processor. If the processor has an instruction that will "jump on zero", then a comparison instruction is not needed; thus saving some execution time. We are talking about saving one instruction per iteration of the loop. In modern processor, that could be in units of nanoseconds. The time saved by this micro-optimization will probably be wasted in other parts of the code, waiting for I/O, or swapping memory, or waiting for other tasks with shared resources (like memory). – Thomas Matthews May 09 '19 at 17:58
  • It also depends of loop content, accessing memory in order might be more cache friendly for example. – Jarod42 May 09 '19 at 21:49
  • Possible duplicate of [Is it faster to count down than it is to count up?](https://stackoverflow.com/questions/2823043/is-it-faster-to-count-down-than-it-is-to-count-up) – Josh Lee May 09 '19 at 22:08

2 Answers2

0

The blog is correct, however, from my experience performance is more complex than changing a few small tweaks.

The important element ain't the order of looping. You would get the same effect when going from -10 to 0. The relevant element here is the comparison with 0. CPUs have a dedicated instruction to compare with 0, while comparison with any other number needs to use a generic comparison.

So for the looping, yes, it will be faster. Though, as mentioned before, a lot more is at play. Compiler optimizations line loop unrolling could remove the complete loop. Almost any operation is more expensive than the looping itself and algorithmic changes can save a lot of time.

For more details on comparison with zero, you can start in following thread: Is comparing to zero faster than comparing to any other number?

JVApen
  • 11,008
  • 5
  • 31
  • 67
-4

its know that this loop last position is 0 so can't check condition. so its direct i-1 and then print

int main()
   {
      int i;
       for( i =10; i--;)
       {
       cout<<i;
       }

     return 0;
   }