-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.sol
43 lines (38 loc) · 1.15 KB
/
test.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
pragma solidity 0.8.15;
contract Test {
// 5 loops, total gas cost == 22138
// 100 loops, total gas cost == 27743
// 1000 loops, total gas cost == 80855
function increment0(uint256 loops) external pure returns (uint256) {
uint256 value;
for (uint256 i; i < loops;) {
unchecked { ++i; }
value = i;
}
return value;
}
// 5 loops, total gas cost == 22051
// 100 loops, total gas cost == 26326
// 1000 loops, total gas cost == 66838
function increment1(uint256 loops) external pure returns (uint256) {
uint256 value;
for (uint256 i;;) {
unchecked { ++i; }
value = i;
if (i >= loops) break;
}
return value;
}
// 5 loops, total gas cost == 22019
// 100 loops, total gas cost == 26294
// 1000 loops, total gas cost == 66806
function increment2(uint256 loops) external pure returns (uint256) {
uint256 value;
for (uint256 i;;) {
unchecked { i += 1; } // i += 1 cheaper than ++i
value = i;
if (i >= loops) break;
}
return value;
}
}