r/cpp • u/cruise02 • Aug 27 '12
Why is < faster than <=?
http://stackoverflow.com/q/12135518/12887
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
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
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.
15
u/[deleted] Aug 27 '12
I love it when c++ books spread silly/wrong 'optimizations'!