为什么气体成本取决于功能输入?
我正在使用混音测试此代码。我想知道为什么功能的执行成本(在气体中)取决于输入x。随着X值的增加,成本似乎增加了12个。我没有找到模式。
// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;
contract Example{
function test (uint x) external pure returns(bool z) {
if(x > 0)
z=true;
else
z=false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
https://github.com/ Wolflo/evm-opcodes/blob/main/gas.md#a0-0-intrinsic-gas
发送给合同的字节确实决定了从链接可以看出的气体成本。
gas_cost += 4 * bytes_zero:每一个零字节的基本成本添加了存储器数据
GAS_COST += 16 * BYTES_NONZERO:每一个非零字节的基本成本添加了GAS,
因此,如果发送0x0001或0x0010,则将花费相同的气体。但是,如果您发送0x0011的价格将比以前的情况高12(16-4)。
https://github.com/wolflo/evm-opcodes/blob/main/gas.md#a0-0-intrinsic-gas
The bytes sent to the contract indeed decides the gas cost as can be seen from the link.
gas_cost += 4 * bytes_zero: gas added to base cost for every zero byte of memory data
gas_cost += 16 * bytes_nonzero: gas added to base cost for every nonzero byte of memory data
so if you send 0x0001 or 0x0010 it will cost the same amount of gas. But if you send 0x0011 it will cost 12(16-4) gas more than the previous case.
在三种情况下收取气体
的计算
,内存使用的使用增加。功能ARG和功能中的局部变量是内存数据。
Gas is charged in three scenarios
The computation of an operation
For contract creation or message calls
An increase in the use of memory. Function args and local variables in functions are memory data. In Ethereum Solidity, what is the purpose of the "memory" keyword?