Created
March 17, 2023 08:35
-
-
Save drgorillamd/d88f697a9508df0ddb205cbbfc35fe09 to your computer and use it in GitHub Desktop.
Quick gas comparison between multiple ways to access struct elements stored in a single word
This file contains 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
pragma solidity 0.8.16; | |
// optim 200 runs | |
contract Test { //27038 | |
struct StructA { | |
uint128 _a; | |
uint128 _b; | |
} | |
StructA structA; | |
constructor() { | |
structA._a = uint128(block.timestamp); | |
structA._b = uint128(1 hours); | |
} | |
function test() external returns (uint) { | |
return structA._a + structA._b; | |
} | |
} | |
contract Test2 { //27050 | |
struct StructA { | |
uint128 _a; | |
uint128 _b; | |
} | |
StructA structA; | |
constructor() { | |
structA._a = uint128(block.timestamp); | |
structA._b = uint128(1 hours); | |
} | |
function test() external returns (uint) { | |
(uint128 _a, uint128 _b) = (structA._a, structA._b); | |
return _a + _b; | |
} | |
} | |
contract Test3 { //27123 | |
struct StructA { | |
uint128 _a; | |
uint128 _b; | |
} | |
StructA structA; | |
constructor() { | |
structA._a = uint128(block.timestamp); | |
structA._b = uint128(1 hours); | |
} | |
function test() external returns (uint) { | |
StructA memory _structA = structA; | |
return _structA._a + _structA._b; | |
} | |
} | |
contract Test4 { //27047 | |
struct StructA { | |
uint128 _a; | |
uint128 _b; | |
} | |
StructA structA; | |
constructor() { | |
structA._a = uint128(block.timestamp); | |
structA._b = uint128(1 hours); | |
} | |
function test() external returns (uint) { | |
StructA storage _structA = structA; | |
return _structA._a + _structA._b; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment