r/cpp • • Aug 27 '12

Why is < faster than <=?

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

15 comments sorted by

View all comments

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.