How efficient is checking x>k?

In what order in terms of speed (possibly some at equal speed) will the following comparisons run (in if statements):
a) x>k
b) x<k (if k is huge, will this be slower?)
c) x==k
d) x!=k
e) x==k || x==k+1

The difference might be unnoticeable, but can you answer this theoretically? Thanks.

Edit: I'm asking out of curiosity.
Last edited on
closed account (48T7M4Gy)
Sounds like a quiz. What do you think?
It depends on how the compiler optimizes the code into machine code. Even without optimizations it can differ from one architecture to another. It all depends on what the underlying machine code is capable of doing and how fast the particular processor can do it.
I am unaware of any processor for which both equality and less-than are not both defined; all but (e) can be rewritten as a single operation.
@Duoas: don't forget the world of embedded devices. I have actually worked with a device (a robot microcontroller for high school robotics classes) that only implemented the basic minimal instructions to be usable. Less-than was a single native operation, but greater-than has to be simulated with multiple instructions. (The compiler for this device also had a 24-bit short long integer type, which, from what I have heard, is not uncommon in the world of microdevices.)
Couldn't you just flip the operands?
helios wrote:
Couldn't you just flip the operands?
You make the assumption that this compiler was smart - flipping the operands is something you have to do yourself. (It's really stupid, I know, but trust me - I timed it and it was a real thing)

Though, it has been years since I worked with it - I could be completely misremembering what I am talking about. All I know is that I facepalmed at the stupidity of this microcontroller.
Last edited on
in some uC the instruction are on the way W op F and you may chose to store the result in W or in F.
W is the special `working' register and F may be any register on memory

so to invert the operands you'll need to
1
2
3
4
5
movf aux //dump W
movwf F,w //load register F into the working register
lesswf aux //do the comparison
   goto  true_case
   goto  false_case
That would be 2 extra instructions.
Or you could compute/load the operands so they end up in the right registers to begin with.
helios wrote:
Or you could compute/load the operands so they end up in the right registers to begin with.
In some cases, doing that could still be less efficient than having a native single-instruction greater-than.
For example?
I guess I meant to say that it may not even be possible to write the code in a way which makes the operands end up in the correct places.

Disclaimer: I don't remember the specifics of the processor, I've only worked with assembly once, I've never worked with machine code. I'm making statements based purely on my intuition.
Well, if the operands are put in that order by a subroutine that's used in several places, I suppose it's better to bite the bullet and flip them than to have two copies of the same subroutine just for that. Otherwise, I fail to see any reason why you couldn't reorder operations to get things in the right places.
helios wrote:
I fail to see any reason why you couldn't reorder operations to get things in the right places.
Right, but doing so may involve adding additional instructions, which is undesirable.
If you have to add or remove instructions then that would be "not reorder", which is what I said I don't see why you'd have to do.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// a+b < c
movwf A, w
addwf B, w //w = a+b
lesswf C
  goto true
  goto false


// a+b > c -> c < a+b
movwf B, w
addwf A, f //a = a+b, destructive but we are only interested in the result
movwf C, w
lesswf A
  goto true
  goto false
greater-than takes me 1 extra instruction than less-than
remember that W is always the left side operand
that's why in line 12 I load register C into W and then compare against A (that has the result of A+B)
Last edited on
I don't suppose it has a leq.
Dang it Duoas

Duoas wrote:
all but (e) can be rewritten as a single operation.


I read that as e can be rewritten as a single operation, and spent the next 5 confusing minutes trying to figure out how xD

Well, if k is the second lowest value of it type, it can be done as x < k +1
Topic archived. No new replies allowed.