-3

I was wondering if > is faster then >= ? I tried to benchmark it but either it takes 0ms or it takes forever. I understand that the difference will be really small but i have to operate on a lot pixels. Can someone tell me what is faster?

clankill3r
  • 9,146
  • 20
  • 70
  • 126
  • 3
    Your question isn't very clear - what language are you talking about? And by referring to pixels are you doing some kind of image manipulation? – Starscream1984 May 02 '14 at 14:53
  • Knuth has a quote that may be applicable here... :P – Wooble May 02 '14 at 14:53
  • These operands will be compiled into machine code which will most likely take the exact same number of cycles to execute. You need to post a concrete example, and tell us on which type you operate... – Flovdis May 02 '14 at 14:54
  • Surely it depends what you are comparing; for example `if (downloadInternet() > 42) { ... }` would take longer than `if (41 > 42) { ... }`... – Droppy May 02 '14 at 14:54
  • 1
    `"I tried to benchmark it but either it takes 0ms or it takes forever"` - Sounds like there's something wrong with your benchmark. Any significant loop through many comparisons is going to take longer than 0ms. And anything that takes "forever" clearly has a logic error. – David May 02 '14 at 14:56
  • @Flovdis: Not *necessarily* true; `>` and `>=` are not freely exchangeable comparisons. Ultimately, it depends on the data and on what you do with it. And how the compiler optimizes it. And perhaps because of caching. (In general.) – Jongware May 02 '14 at 14:56
  • If "the difference is really small" *but* you have to operate on a lot of pixels anyway, then you actually have a good benchmark test right there. Please *do* share the results. – Jongware May 02 '14 at 14:59

1 Answers1

4

Both comparisons will be compiled to machine instructions like BLT (branch on less than) or BLE (branch on less equal), which check some status bits like BLT: N-V + -NV (negative & not overflow or not negative and overflow) or BLE: Z + N-V + -NV (zero or negative & not overflow or not negative and overflow).
These instruction take normally exactly the same time, so they are equally fast.

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116