Closed code423n4 closed 2 years ago
++i costs less gas as compared to i++ for unsigned integer, as per-increment is cheaper(its about 5 gas per iteration cheaper)
i++ increments i and returns initial value of i. Which means
uint i = 1; i++; // ==1 but i ==2
But ++i returns the actual incremented value:
uint i = 1; ++i; // ==2 and i ==2 , no need for temporary variable here
In the first case, the compiler has create a temporary variable (when used) for returning 1 instead of 2.
contracts/NestedRecords.sol:78: tokenIndex++;
https://www.reddit.com/r/ethdev/comments/tcwspw/i_vs_i_gas_efficiency/
Use Preincrement(++i) instead of Postincrement(i++) in code.
Duplicated of #2 at For loop optimizaion
Warden submitted multiple Gas Optimizations. Will not be judged.
Preincrement Costs less gas(++i) as compared to postincrement(i++)
++i costs less gas as compared to i++ for unsigned integer, as per-increment is cheaper(its about 5 gas per iteration cheaper)
i++ increments i and returns initial value of i. Which means
But ++i returns the actual incremented value:
In the first case, the compiler has create a temporary variable (when used) for returning 1 instead of 2.
Instances:
Reference:
https://www.reddit.com/r/ethdev/comments/tcwspw/i_vs_i_gas_efficiency/
Remediation:
Use Preincrement(++i) instead of Postincrement(i++) in code.