Gas Costs for Solidity Built-in Functions
| Operation | Gas Cost | Notes |
|---|---|---|
| Simple Arithmetic | ||
+, -, *, /, % | 3–5 gas | Basic integer math. Division is more expensive. |
** (exponentiation) | 10–100+ gas | Cost scales with exponent size. |
++, -- | 3–5 gas | Increment/decrement. |
| Storage Operations | ||
SSTORE (write new value) | 20,000–22,100 gas | 22,100 for new storage slot, 20,000 for updating existing non-zero value. |
SSTORE (set to zero) | 500–5,000 gas | Gas refund may apply. |
SLOAD (read storage) | 2,100 gas | Reading from storage. |
| Memory Operations | ||
MLOAD (read memory) | 3 gas | |
MSTORE (write memory) | 3–6 gas | |
| Ether Transfers | ||
address.transfer() | 2,300 gas stipend | Fixed gas, reverts on failure. |
address.send() | 2,300 gas stipend | Returns bool, does not revert. |
address.call{value: x}() | All remaining gas | Flexible but risky (reentrancy). |
| Hashing & Cryptography | ||
keccak256() | 30–60 gas | Cost depends on input size. |
sha256() | 60+ gas | More expensive than keccak256. |
| Contract Operations | ||
new Contract() | 32,000+ gas | Base cost + deployment bytecode execution. |
selfdestruct(address) | 5,000 gas | Refunds may apply. |
| Logging (Events) | ||
emit Event() | 375–2,000+ gas | Base cost + 375 gas per topic + 8 gas per byte of data. |
| Control Flow | ||
require() / revert() | 10–30 gas | Early termination with message costs more. |
assert() | 10–30 gas | Similar to require but for internal errors. |
| Function Calls | ||
External call (contract.func()) | 700+ gas | Base cost + function execution. |
Internal call (this.func()) | 100–300 gas | Cheaper than external calls. |
Key Notes
- Gas Costs Are Approximate:
- Actual costs depend on compiler optimizations, EVM upgrades, and context (e.g., cold vs. warm storage access).
- Storage vs. Memory:
- Storage (
SSTORE/SLOAD) is 1,000x+ more expensive than memory (MSTORE/MLOAD).
- Storage (
- Ether Transfers:
transferandsendlimit gas to 2,300 (preventing reentrancy but risky for contracts).callforwards all gas (use with checks-effects-interactions).
- Refunds:
- Setting storage to zero (
SSTOREto0) may refund up to 4,800 gas.
- Setting storage to zero (
Example Gas Calculation
function example() public {
uint a = 1; // ~3 gas (memory write)
a += 1; // ~3 gas (arithmetic)
emit Log(a); // ~500 gas (1 topic + data)
require(a > 0, "!"); // ~25 gas
}
Total: ~531 gas (excluding function call overhead).
For precise gas estimation, use:
- Hardhat:
hardhat-gas-reporterplugin. - Remix: Gas meter in the debugger.
- Etherscan: Check gas used in confirmed transactions.