Skip to content

Instantly share code, notes, and snippets.

@valterlobo
Created September 22, 2022 13:11
Show Gist options
  • Save valterlobo/b94299a4dad0565c6570e54599a59b4b to your computer and use it in GitHub Desktop.
Save valterlobo/b94299a4dad0565c6570e54599a59b4b to your computer and use it in GitHub Desktop.
Solidity : gas array
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
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 unsafe_inc(uint x) private pure returns (uint) {
unchecked { return x + 1; }
}
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;
}
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment