-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path77_GasOptimization.sol
More file actions
49 lines (41 loc) · 1.29 KB
/
Copy path77_GasOptimization.sol
File metadata and controls
49 lines (41 loc) · 1.29 KB
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
44
45
46
47
48
49
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
contract Gas_Test {
uint[] public arrayFunds;
uint public totalFunds;
constructor() {
arrayFunds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
}
function optionA() external {
for (uint i = 0; i < arrayFunds.length; i++) {
totalFunds = totalFunds + arrayFunds[i];
}
}
function optionB() external {
uint _totalFunds;
for (uint i = 0; i < arrayFunds.length; i++) {
_totalFunds = _totalFunds + arrayFunds[i];
}
totalFunds = _totalFunds;
}
function optionC() external {
uint _totalFunds;
uint[] memory _arrayFunds = arrayFunds;
for (uint i = 0; i < _arrayFunds.length; i++) {
_totalFunds = _totalFunds + _arrayFunds[i];
}
totalFunds = _totalFunds;
}
// Optional
// function unsafe_inc(uint x) private pure returns (uint) {
// unchecked { return x + 1; }
// }
// function optionD() external {
// uint _totalFunds;
// uint[] memory _arrayFunds = arrayFunds;
// for (uint i =0; i < _arrayFunds.length; i = unsafe_inc(i)){
// _totalFunds = _totalFunds + _arrayFunds[i];
// }
// totalFunds = _totalFunds;
// }
}