1/5
_Follow along with this video:_ --- ### Comparing Gas Costs Assuming all our tests are passing, one of the coolest things this set up allows us to do is directly compare the gas costs of each of our tests using the command ```bash forge snapshot ``` This is going to run all of our tests, but keep a record of gas costs in a local file named `.gas-snapshot`. ::image{src='/formal-verification-1/70-huff-yul-and-solidity-gas-comparison/huff-yul-and-solidity-gas-comparison1.png' style='width: 100%; height: auto;'} This output file lets us compare the gas costs of each test performed for each of our implementations. We can see things like: ``` - HorseStoreHuff:testReadValue() (gas: 7419) - HorseStoreSolc:testReadValue() (gas: 7525) ``` Huff is more gas efficient than Solidity! --- ``` - HorseStoreHuff:testWriteValue(uint256) (runs:256, u: 27008, ~: 28097) - HorseStoreSolc:testWriteValue(uint256) (runs:256, u: 27251, ~: 28340) ``` Huff wins again! Let's look at one comparison from HorseStoreV2: --- ``` - HorseStoreHuffV2:testFeedingHorseUpdatesTimestamps() (gas: 106119) - HorseStoreSolcV2:testFeedingHorseUpdatesTimestamps() (gas: 115149) ``` Wow! It's nearly 10,000 more gas in raw Solidity than it is in raw Huff. When looking at comparisons like this, it's easy to begin to see the advantages of coding in low level languages like Huff and Yul. Always remember the trade-offs that are made to save this gas however. This saved gas we see is a product of all the checks we skipped the implementation of in Huff, checks meant to keep people safe. We skipped checking for msg.value and call data length for example. These checks exist for a reason and we should be confident and conscientious when we choose to omit them.
A comprehensive guide to comparing gas costs in Solidity, Huff, and Yul - This lesson demonstrates the ability to compare the different gas costs of the same smart contract when coded in Solidity, Huff, and Yul. We learn how to leverage Foundry to run gas snapshots, and understand how to analyze the results.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Assembly
Writing smart contracts using Huff and Yul
Ethereum Virtual Machine OPCodes
Formal verification testing
Smart contract invariant testing
Halmos, Certora, Kontrol
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Josselin Feist
Head of Blockchain at Trail of Bits
Last updated on January 17, 2025
Solidity Developer
Assembly and Formal VerificationDuration: 30min
Duration: 4h 38min
Duration: 3h 57min
Duration: 1h 56min
Course Overview
About the course
Assembly
Writing smart contracts using Huff and Yul
Ethereum Virtual Machine OPCodes
Formal verification testing
Smart contract invariant testing
Halmos, Certora, Kontrol
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Josselin Feist
Head of Blockchain at Trail of Bits
Last updated on January 17, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar