Reputation: 519
This question is mostly directed at compiled programming languages. It is purely out of curiosity because I believe the gain of performance in using one of the two operators would be so very small.
Consider a for-loop where when you meet a certain condition, you want to store true in a boolean:
b = false
for i in 1..N:
if someCondition(i):
b = true
moreThatNeedsToBeDone(i)
endfor
Now consider the same for loop with OR EQUAL instead
b = false
for i in 1..N:
if someCondition(i):
b |= true
moreThatNeedsToBeDone(i)
endfor
If the condition is met more than once, would the latter be faster in theory? Or atleast, would it do fewer operations? In general, the OR EQUAL evaluates the variable and if it is true, then it doesn't do anything hence there is no extra assignment compared to the EQUAL operator where it would STORE true multiple times. But writing this, I realize the OR EQUAL adds an extra operation anyways in order to evaluate/read the current value of the variable. So which would be faster or have fewer operations to do?
Upvotes: 0
Views: 56
Reputation: 16146
Most current compilers optimize to the best run time, meaning the resulting assembler or machine code would probably be identical. You could out of interest take a peek at the assembly generated for the two versions. I bet they are identical.
If the compiler would translate literally, the |=
operation will not involve evaluating the variable to know if OR'ing the value should be done or not. The compiler will simply emit an or
instruction as that would be equivalent and faster than first checking the variable (which could end up clearing the instruction pipeline).
Without optimization, the assembly generated could be (ax
is a processor register):
or ax,1 ; for b|=true
mov ax,1 ; for b=true
I doubt on current processors this makes any difference in execution speed at all. Even if it would make a difference, we're talking about such a marginal optimization versus load times from memory to cache to processor/registers, or versus thread switches/process switches on CPU's, or versus branch misprediction in pipelined microprocessors and such.
Upvotes: 1