Created
June 3, 2022 21:01
-
-
Save 0xmikko/19358aad2496fbc56f8bc557265eabf2 to your computer and use it in GitHub Desktop.
Solidity gas optimisation example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity ^0.8.10; | |
contract Test { | |
// Gas used for empy function: 22094 | |
function blank(uint16) public pure returns (string memory) {} | |
// Gas used: 31922 - 22094 = 9828 | |
function toBinary1(uint16 n) public pure returns (string memory) { | |
require(n < 1024); | |
bytes memory output = new bytes(10); | |
for (uint8 i = 0; i < 10; ++i) { | |
output[9 - i] = (n % 2 == 1) ? bytes1("1") : bytes1("0"); | |
n /= 2; | |
} | |
return string(output); | |
} | |
// Gas used: 31638 - 22094 = 9544 | |
function toBinary2(uint16 n) public pure returns (string memory) { | |
require(n < 1024); | |
bytes memory output = new bytes(10); | |
for (uint256 i = 0; i < 10; ++i) { | |
output[9 - i] = (n % 2 == 1) ? bytes1("1") : bytes1("0"); | |
n /= 2; | |
} | |
return string(output); | |
} | |
// Gas used: 30408 - 22094 = 8314 | |
function toBinary3(uint16 n) public pure returns (string memory) { | |
require(n < 1024); | |
bytes memory output = new bytes(10); | |
uint256 i; | |
while (i < 10) { | |
output[9 - i] = (n % 2 == 1) ? bytes1("1") : bytes1("0"); | |
n /= 2; | |
unchecked { | |
++i; | |
} | |
} | |
return string(output); | |
} | |
// Gas used: 24081 - 22094 = 1987 | |
function toBinary4(uint16 n) public pure returns (string memory) { | |
require(n < 1024); | |
bytes memory output = new bytes(10); | |
uint256 i = 9; | |
while (i != 0) { | |
output[i] = uint256(n) & (1 << i) == 0 ? bytes1("0") : bytes1("1"); | |
unchecked { | |
--i; | |
} | |
} | |
return string(output); | |
} | |
// Gas used: 24016- 22094 = 1922 | |
function toBinary5(uint256 n) public pure returns (string memory) { | |
require(n < 1024); | |
bytes memory output = new bytes(10); | |
uint256 i = 9; | |
while (i != 0) { | |
output[i] = n & (1 << i) == 0 ? bytes1("0") : bytes1("1"); | |
unchecked { | |
--i; | |
} | |
} | |
return string(output); | |
} | |
// Gas used: 23552 - 22094 = 1458 | |
function toBinary6(uint256 n) public pure returns (string memory) { | |
require(n < 1024); | |
bytes memory output = new bytes(10); | |
output[0] = n & 1 == 0 ? bytes1("0") : bytes1("1"); | |
output[1] = n & 2 == 0 ? bytes1("0") : bytes1("1"); | |
output[2] = n & 4 == 0 ? bytes1("0") : bytes1("1"); | |
output[3] = n & 8 == 0 ? bytes1("0") : bytes1("1"); | |
output[4] = n & 16 == 0 ? bytes1("0") : bytes1("1"); | |
output[5] = n & 32 == 0 ? bytes1("0") : bytes1("1"); | |
output[6] = n & 64 == 0 ? bytes1("0") : bytes1("1"); | |
output[7] = n & 128 == 0 ? bytes1("0") : bytes1("1"); | |
output[8] = n & 256 == 0 ? bytes1("0") : bytes1("1"); | |
output[9] = n & 512 == 0 ? bytes1("0") : bytes1("1"); | |
return string(output); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment