0Pricing
Web3 & DApp Development Fundamentals · Lesson

Measuring Gas

Profiling tools.

Measure, Don't Guess

Gas optimization without measurement is guesswork. The compiler, opcode costs, and access patterns interact in non-obvious ways, so an intuitive change can sometimes make things worse.

Always profile before and after. A change that does not reduce measured gas is not an optimization, no matter how clever it looks.

Forge Gas Reports

Foundry's forge test --gas-report prints a per-function table of min, average, median, and max gas usage across all your tests. It is the fastest way to spot expensive functions.

$ forge test --gas-report

| Function    | min   | avg   | median | max   | # calls |
| transfer    | 28012 | 34521 | 34521  | 51012 | 12      |
| approve     | 24300 | 24300 | 24300  | 24300 | 5       |

All lessons in this course

  1. Gas Cost Model
  2. Storage Optimization
  3. Loop and Calldata Tricks
  4. Measuring Gas
← Back to Web3 & DApp Development Fundamentals