Skip to content

Instantly share code, notes, and snippets.

@marsrobertson
Created November 16, 2022 18:38
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 marsrobertson/44ef0abc564237da5db6c620140928e2 to your computer and use it in GitHub Desktop.
Save marsrobertson/44ef0abc564237da5db6c620140928e2 to your computer and use it in GitHub Desktop.
Does inheritance in Solidity cost gas?
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Base {
uint256 number = 69420;
function operation(uint256 addme) public view returns (uint256) {
return number + addme;
}
}
contract Inheritance is Base {
}
contract TestGasUsage1000xLoop {
function testBase() public {
Base base = new Base();
for (uint i=0; i<100; i++) {
base.operation(i);
}
}
function testInheritance() public {
Inheritance inheritance = new Inheritance();
for (uint i=0; i<100; i++) {
inheritance.operation(i);
}
}
}

testBase

transaction cost 349253 gas

status	true Transaction mined and execution succeed
transaction hash	0x2066de6cc82c6ff8e68a0699ec8a66932bd8cb90227cc9d8c4125743563fdd85
from	0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
to	TestGasUsage1000xLoop.testBase() 0xd9145CCE52D386f254917e481eB44e9943F39138
gas	401641 gas
transaction cost	349253 gas 
execution cost	349253 gas 
input	0x09e...c1383
decoded input	{}
decoded output	{}
logs	[]
val	0 wei

testInheritance

transaction cost 349275 gas

status	true Transaction mined and execution succeed
transaction hash	0x362b7d5a3987d8e508d218ed6200a357821a4645d806b7b9d9f05e467bd0c85b
from	0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
to	TestGasUsage1000xLoop.testInheritance() 0xd9145CCE52D386f254917e481eB44e9943F39138
gas	401667 gas
transaction cost	349275 gas 
execution cost	349275 gas 
input	0xd93...746d5
decoded input	{}
decoded output	{}
logs	[]
val	0 wei

Total saving: 22 gas

@marsrobertson
Copy link
Author

Remix

Screenshot

image

@marsrobertson
Copy link
Author

Actually I've reduced the loop to 100 as 1000 froze page and crashed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment