I've been working with python and pygame recently, and I was wondering, is it more efficient, processor/RAM wise to use if x != y: x = y
or x = y
in my main loop? I understand that the difference would be so small that it would make next to no difference, but I am still curious.
Asked
Active
Viewed 114 times
-1

Cedric Hutchings
- 154
- 8
-
Not sure what you're getting at here... the first statement has 2 components, an if statement and an assignment, the second statement just has the same assignment. So, wouldn't it make sense that the second is faster? – Chris Sprague Jun 17 '16 at 21:01
-
Not necesarily: aren't comparisons faster than assignments? Then if "almost every time" x == y then it'd probably faster to check first, wouldn't it? – Dleep Jun 17 '16 at 21:03
1 Answers
0
x = y
doesn't require a comparison, and a comparison can be resource costy sometimes.
So I would say that just x = y
is better.

Yotam Salmon
- 2,400
- 22
- 36
-
Yes, but the OP's point is that sometimes the comparison is false and then you don't need an assignment. – jonrsharpe Jun 17 '16 at 21:05
-
I see his point, but comparison, as far as I know, is more expensive (in terms of resources) than assignment. – Yotam Salmon Jun 17 '16 at 21:06