0Pricing
Web3 & DApp Development Fundamentals · บทเรียน

การวัดแก๊ส

เครื่องมือสร้างโปรไฟล์

การวัดแก๊ส เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web3 & DApp Development Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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       |

Gas Snapshots

forge snapshot writes the gas cost of every test to a .gas-snapshot file. Commit it to version control, then run forge snapshot --diff to see exactly how a change affected gas.

This turns gas regressions into reviewable diffs in pull requests.

$ forge snapshot
$ forge snapshot --diff

testTransfer() (gas: -1240 (-3.6%))
testMint() (gas: +85 (+0.2%))

Measuring in Tests

For fine-grained measurement, read gasleft() before and after a call inside a test. The difference is the gas consumed by that block of code.

uint256 before = gasleft();
target.doWork();
uint256 used = before - gasleft();
emit log_named_uint("gas used", used);

Verbose Traces

Running tests with -vvvv shows a full execution trace including the gas cost of each external call and opcode group. This reveals where gas is spent, not just the total.

$ forge test --match-test testTransfer -vvvv

[34521] Token::transfer(bob, 100)
  emit Transfer(from: alice, to: bob, value: 100)
  return: true

The Optimizer Setting

The Solidity optimizer changes runtime and deployment gas. The runs parameter is a hint about how often functions are called.

  • Low runs (e.g. 1) optimizes for cheap deployment
  • High runs (e.g. 10000) optimizes for cheap repeated calls

Set it in foundry.toml and always measure with the optimizer enabled, since it reflects production behavior.

# foundry.toml
[profile.default]
optimizer = true
optimizer_runs = 10000

Comparing Implementations

To compare two approaches, write a test that exercises each under identical conditions and inspect the gas report side by side.

Keep inputs the same and isolate the change to one variable, otherwise you cannot attribute the gas difference to the optimization you made.

function testNaive() public { c.sumNaive(data); }
function testOptimized() public { c.sumOptimized(data); }
// compare both rows in --gas-report

Beware Warm vs Cold Skew

Gas reports can mislead because the first access in a test is cold and later ones are warm. A function called once in setup may show a higher cold cost than the same function later.

Look at min, median, and max columns together rather than a single average, and design tests that reflect real-world access patterns.

Etherscan and Real Receipts

On-chain transaction receipts report the actual gasUsed. Etherscan and block explorers display this for every transaction, letting you validate your local measurements against mainnet reality.

The eth_estimateGas RPC method gives a pre-flight estimate, though it adds a safety margin and may differ from the real execution cost.

CI Gas Regression Gates

Wire forge snapshot --check into continuous integration. It fails the build if any test's gas exceeds the committed snapshot beyond a tolerance.

This prevents accidental gas regressions from slipping into production and makes gas a first-class quality metric.

$ forge snapshot --check
# exits non-zero if gas increased vs committed snapshot

A Measurement Workflow

A solid gas workflow:

  • Write tests covering the hot paths
  • Record a baseline with forge snapshot
  • Make one optimization at a time
  • Run forge snapshot --diff to confirm the gain
  • Gate regressions in CI with --check

Discipline beats cleverness.

Quick Check

Which Foundry command produces a per-function table of min, average, and max gas usage?

Recap

You learned to measure gas:

  • forge test --gas-report for per-function tables
  • forge snapshot --diff to track changes
  • gasleft() for fine-grained measurement
  • Verbose traces reveal where gas goes
  • Gate regressions in CI with --check

This completes the Gas Optimization course. Next: the Foundry toolkit in depth.

คำถามที่พบบ่อย

บทเรียน “การวัดแก๊ส” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การวัดแก๊ส” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web3 & DApp Development Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การวัดแก๊ส”

เครื่องมือสร้างโปรไฟล์ คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web3 & DApp Development Fundamentals หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web3 & DApp Development Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การวัดแก๊ส” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Web3 & DApp Development Fundamentals นี้ได้ไหม

ได้ บทเรียน Web3 & DApp Development Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. แบบจำลองต้นทุนแก๊ส
  2. การเพิ่มประสิทธิภาพพื้นที่จัดเก็บ
  3. เทคนิคเกี่ยวกับลูปและ Calldata
  4. การวัดแก๊ส
← กลับไปที่ Web3 & DApp Development Fundamentals