What is the more gas-efficient expression to check for non-equality? Is it n != 0
, or n > 0
?
1 Answer
According to the Ethereum Yellow Paper (Appendix G - Fee Schedule), the gas costs for the relevant opcodes are as follows:
The first expression (n != 0
) utilizes both the EQ
and the ISZERO
opcodes as follows: iszero(eq(x, 0))
. While the second expression (n > 0
) only utilizes the GT
opcode as follows: gt(x, 0)
.
Therefore, the expression n > 0
is more gas-efficient than n != 0
by 3 gas units.