为什么气体成本取决于功能输入?

发布于 2025-02-06 08:51:55 字数 311 浏览 1 评论 0 原文

我正在使用混音测试此代码。我想知道为什么功能的执行成本(在气体中)取决于输入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;
    }
}

I was testing this code using Remix. I was wondering why the execution cost of the function (in gas) depends on the input x. The cost seems to increase in multiples of 12 as the value of x increases. I haven't found a pattern.

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌能量女王 2025-02-13 08:51:55

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.

贱人配狗天长地久 2025-02-13 08:51:55

在三种情况下收取气体

  • 的计算

    的计算

  • ,内存使用的使用增加。功能ARG和功能中的局部变量是内存数据。


Gas is charged in three scenarios

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文