# 以太坊如何估計估算計算gas？

remix瀏覽器下方有個可執行的log頁面，可以detail以及debug，非常方便。

transaction cost指的是將交易送至ethereum blockchain所耗費的cost，是基於data size的大小，部署合約時就是基於合約內容的大小．
execution cost指的是虛擬機器(VM)執行所需的cost，而在部署合約時，會去執行建構子以及一些初始化的工作．

``````contract Test {
bytes32 public tmp;
function test(
bytes32 input,
uint num
) constant returns (bytes32){
bytes32 result = input;
for(uint i = 0; i < num; i  ) {
result = sha3(result);
}
}
function set(bytes32 input, uint num) {
tmp = test(input, num);
}
}
``````

``````set(“0x63d7db5ce060b288ecf5390594d5969bc1a206ceeb24df31cffcc8876df5e44b”, 10)
transaction cost:30628
execution cost:6988
set(“0x63d7db5ce060b288ecf5390594d5969bc1a206ceeb24df31cffcc8876df5e44b”, 1000)
transaction cost:196022
execution cost:172318
test(“0x63d7db5ce060b288ecf5390594d5969bc1a206ceeb24df31cffcc8876df5e44b”, 10)
transaction cost:25663 (cost only applies when called by a contract)
execution cost:2023 (cost only applies when called by a contract)
test(“0x63d7db5ce060b288ecf5390594d5969bc1a206ceeb24df31cffcc8876df5e44b”, 1000)
transaction cost:191057(cost only applies when called by a contract)
execution cost:167353(cost only applies when called by a contract)
``````

ps:用transaction cost減去execution cost的話1, 3得到23640，2, 4得到23704

1.適合區塊鏈新手的以太坊DApp開發：