r/cpp • • Aug 27 '12

Why is < faster than <=?

http://stackoverflow.com/q/12135518/1288
6 Upvotes

15 comments sorted by

15

u/[deleted] Aug 27 '12

I love it when c++ books spread silly/wrong 'optimizations'!

4

u/Xetick Aug 27 '12

Specially when they are truly and utterly pointless even if they were true. Say the gain would be 1 clock cycle or 2. What program would actually benefit from it compared to changing the actual algorithm or some other part of the program.

3

u/[deleted] Aug 28 '12

Welcome to the world of high-frequency trading.

1

u/Gaminic Sep 04 '12

Quite wortwhile, actually. It's a very common operation and can be executed millions or billions of times in a simple algorithm. Those can add up. It's not going to fix an O(an ) algorithm, but if you expect a piece of code will be used tremendously often, these low-level optimizations can make a difference.

That being said, most (if not all) of this "minor code magics" are done by the compiler these days. No need to convert 2n multiplications to bitshifts or reverse loop orders. If it is consistently faster, your compiler will do it for you. Any other low level optimizations are most likely not worth the obfuscation.

1

u/JustPlainRude Sep 09 '12

Any program with real-time constraints.

0

u/[deleted] Aug 27 '12

[deleted]

6

u/nexuapex Aug 27 '12

The tags under the original question.

0

u/[deleted] Aug 27 '12

[deleted]

3

u/ponchedeburro Aug 27 '12

I fail to understand your logic.

He doesn't mention it posting here, because this is already a cpp subreddit. It is kinda implicit that he wont be linking java here.

0

u/[deleted] Aug 27 '12

[deleted]

4

u/greyfade Aug 27 '12

What part of the question tags has you so confused? Have you never used StackOverflow?

The question was tagged C++ by the writer of the question. Therefore, the question applies specifically to C++ and is to be answered from that perspective. That's how SO works.

5

u/Eoinoc Aug 27 '12

Ok I'm sorry, I was wrong.

1

u/Eoinoc Aug 27 '12

Ok I'm sorry, I was wrong.

1

u/[deleted] Aug 27 '12

It's in the C++ section of stack overflow, also on a c++ subreddit. And I was being haterish, to all of the people out there who post/write/perpetuate silly non-true 'facts' about code optimization.

7

u/Hello71 Aug 28 '12

tl;dr because it isn't

3

u/iissqrtneg1 Aug 27 '12

This makes me wonder if looping backwards is actually more performent. Probably not, too lazy to check.

10

u/mcmcc #pragma once Aug 27 '12

On some platforms, it is. I remember years ago being encouraged by HP engineers to write indexed loops backwards (i.e. towards zero) because the compares against zero were slightly faster on PA-RISC chips.

7

u/[deleted] Aug 27 '12

If you're talking about ++i vs --i, there are no difference. However, if you're accessing a large block of memory in a loop, looping forward vs backward could make a big different depends on how smart your CPU is about pre-fecting next cache line.

1

u/[deleted] Dec 13 '12

The top answer was provided 3 minutes after the initial question was asked. Something fishy going on - it would have taken more than 3 minutes to write all of that text.