In test.sol there are 3 functions with the total gas cost in comments above them:
-
increment0 : The unchecked style for loop
-
increment1 : A cheaper more optimized version of increment0
loop count | gas cost |
---|---|
5 | 22138 |
100 | 27743 |
1000 | 80855 |
loop count | gas cost |
---|---|
5 | 22051 |
100 | 26326 |
1000 | 66838 |
loop count | gas cost |
---|---|
5 | 22019 |
100 | 26294 |
1000 | 66806 |
OPCODE | GAS |
---|---|
JUMPDEST | 1 gas, one time cost. wont count this in total. |
DUP4. | 3 gas |
DUP2 | 3 gas |
LT | 3 gas |
ISZERO | 3 gas |
PUSH2 0112 | 3 gas |
JUMPI | 10 gas |
DUP1 | 3 gas |
PUSH1 01 | 3 gas |
ADD | 3 gas |
SWAP1 | 3 gas |
POP | 2 gas |
DUP1 | 3 gas |
SWAP2 | 3 gas |
POP | 2 gas |
PUSH2 00fc | 3 gas |
JUMP | 8 gas |
OPCODE | GAS |
---|---|
JUMPDEST | 1 gas, one time on cost. wont count this in total |
DUP1 | 3 gas |
PUSH1 01 | 3 gas |
ADD | 3 gas |
SWAP1 | 3 gas |
POP | 2 gas |
DUP1 | 3 gas |
SWAP2 | 3 gas |
POP | 2 gas |
DUP4 | 3 gas |
DUP2 | 3 gas |
GT | 3 gas |
ISZERO | 3 gas |
PUSH2 0122 | 3 gas |
JUMPI | 10 gas |
So when we declare our loop break condition in the for loop, we have check at the beginning of every loop, followed by a JUMPI which is a conditional jump that basically says if this is false jump out of the loop but if its true jump into the loop. And if its true we continue through the loop and then hit the end and have to hit an additonal jump to get back to the top of the loop. But in the new way we just continue through the loop and hit our condition at the end and then hit only one JUMPI which says either jump back to the top of the loop or just jump out of the loop. All in all saving us 11 gas a loop, plus possibly some additional gas on the for loop setup
Now for this to work your if statement with the loop break has to be the very last thing in your loop. You have to increment i then check the conditional