Skip to content

Instantly share code, notes, and snippets.

@akolotov
Last active April 25, 2018 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akolotov/14fddcec8afaa33e786f3d5274bae2db to your computer and use it in GitHub Desktop.
Save akolotov/14fddcec8afaa33e786f3d5274bae2db to your computer and use it in GitHub Desktop.
Evaluation of gas usage when duplicates discovery implemented with simple arrays (O(n^2))

Array of different size was sent to hasEnoughValidSignatures() method in the set of experiments. As soon as the transactions was mined, the transaction receipt was get to extract gasUsed value.

num of elements gas usage by tx
4 51885
6 58591
8 66561
10 75796
12 86230
14 97993
16 111019
18 125246
20 140801
pragma solidity ^0.4.23;
contract Test {
mapping (uint256 => uint256) public gasUsed;
function addressArrayContains(address[] array, address value) internal pure returns (bool) {
for (uint256 i = 0; i < array.length; i++) {
if (array[i] == value) {
return true;
}
}
return false;
}
function hasEnoughValidSignatures(address[] _vs) public {
address[] memory encounteredAddresses = new address[](_vs.length);
//gasleft is used for nothing just to change something in the storage
uint256 current = gasleft();
for (uint8 i = 0; i < _vs.length; i++) {
address recoveredAddress = _vs[i];
if (addressArrayContains(encounteredAddresses, recoveredAddress)) {
//in the test it never should be here
gasUsed[_vs.length] = 2**256-1;
}
encounteredAddresses[i] = recoveredAddress;
}
gasUsed[_vs.length] = current - gasleft();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment