每次函数调用后获取准确的 Gas 成本

发布于 2025-01-13 08:44:20 字数 1614 浏览 1 评论 0原文

我在 Solidity 中实现了一个动态数组供我自己使用。以下是我的实现。我可以通过点击remix控制台中的调试消息来获取每个函数调用的准确gas成本。然而,手动获取 Gas 成本很乏味,我在想是否可以编写另一个 Solidity 脚本来获取函数调用的 Gas 成本。例如,我想获取连续调用push API 10000次的gas费用。 x 轴应为 API 调用次数,y 轴应为最多 i 次 API 调用的累积 Gas 成本。 Solidity 中有内置函数可以帮助我做到这一点吗?

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract Array {
    // Several ways to initialize an array
    uint[] public arr;
    uint[] public arr2 = [1, 2, 3];
    // Fixed sized array, all elements initialize to 0
    uint[10] public myFixedSizeArr;

    function get(uint i) public view returns (uint) {
        return arr[i];
    }

    // Solidity can return the entire array.
    // But this function should be avoided for
    // arrays that can grow indefinitely in length.
    function getArr() public view returns (uint[] memory) {
        return arr;
    }

    function push(uint i) public {
        // Append to array
        // This will increase the array length by 1.
        arr.push(i);
    }

    function pop() public {
        // Remove last element from array
        // This will decrease the array length by 1
        arr.pop();
    }

    function getLength() public view returns (uint) {
        return arr.length;
    }

    function remove(uint index) public {
        // Delete does not change the array length.
        // It resets the value at index to it's default value,
        // in this case 0
        delete arr[index];
    }

    function examples() external {
        // create array in memory, only fixed size can be created
        uint[] memory a = new uint[](5);
    }
}

I implemented a dynamic array in solidity for my own use. The following is my implementation. I can get the accurate gas cost of each function call by clicking the debug message in the console of remix. However, manually getting gas costs is tedious and I'm thinking if I can write another solidity script to get the gas cost of my function call. For example, I want to get the gas cost of consecutively calling push API for 10000 times. The x-axis should be the number of API calls, and the y axis should be the cumulative gas cost up to i API calls. Is there any built-in function in solidity to help me do that?

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract Array {
    // Several ways to initialize an array
    uint[] public arr;
    uint[] public arr2 = [1, 2, 3];
    // Fixed sized array, all elements initialize to 0
    uint[10] public myFixedSizeArr;

    function get(uint i) public view returns (uint) {
        return arr[i];
    }

    // Solidity can return the entire array.
    // But this function should be avoided for
    // arrays that can grow indefinitely in length.
    function getArr() public view returns (uint[] memory) {
        return arr;
    }

    function push(uint i) public {
        // Append to array
        // This will increase the array length by 1.
        arr.push(i);
    }

    function pop() public {
        // Remove last element from array
        // This will decrease the array length by 1
        arr.pop();
    }

    function getLength() public view returns (uint) {
        return arr.length;
    }

    function remove(uint index) public {
        // Delete does not change the array length.
        // It resets the value at index to it's default value,
        // in this case 0
        delete arr[index];
    }

    function examples() external {
        // create array in memory, only fixed size can be created
        uint[] memory a = new uint[](5);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

怕倦 2025-01-20 08:44:20

您可以使用 msg.gas 并将值存储到变量中,当合约执行时,气体量将被存储。

You can use msg.gas and store the value to a variable, when the contract is executed the gas amount will be stored.

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