Which is more efficient? checking == or just changing a variable?

Imagine I had a variable called X. Say every 5 seconds, I wanted to make X = true. (it can be true or false between these 5 seconds, but gets reset to true when 5 seconds up).

It would be more efficient to check if the value is already true, and if not, reassign it to true? Or just X = true?

In other words, what will work faster?

if(x==false){
    x = true;
}

against

x = true;

On the one hand, the first program will not mutate a variable if it is not needed. On the other hand, the second program does not need to check that X is equal; he plunges straight.

+3
source share
4
  • . , . .
  • , . .
  • .
  • , .
  • , , , , .
+8

:)

x == false 90% , x .

, , , , , .

+4

, , - , .

+1

/:

- , . , , .

, . , , .

, - ( , ). - x=true, .

, , , , , x=true ( ).


:

- . , 0 (x = false) , - , , x . , , , x .

, . , 0- (, , ..) , . 0- , ( true), 0- . , , , , .

+1

Source: https://habr.com/ru/post/1749972/


All Articles