小编Tye*_*Rik的帖子

在 Solidity 中从一个数组复制到另一个数组的最佳实践是什么?

我正在尝试通过优化代码来节省汽油。然而,我突然想知道在 Solidity 中从一个数组复制到另一个数组的最佳实践是什么。

我提出两个选择。一种是通过指针复制(我猜),另一种是使用 for 循环。

TestOne.sol

contract TestContract {
    uint32[4] testArray;

    constructor(uint32[4] memory seeds) {
        testArray = seeds; // execution costs: 152253
    }

    function Show() public returns (uint32[4] memory) {
        return testArray;
    }
}
Run Code Online (Sandbox Code Playgroud)

测试二.sol

contract TestContract {
    uint32[4] testArray;

    constructor(uint32[4] memory seeds) {
        for(uint i = 0; i < 4; i++) {
            testArray[i] = seeds[i];  // execution costs: 150792
        }
    }

    function Show() public returns (uint32[4] memory) {
        return testArray;
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用 Remix(以太坊在线 IDE)、0.8.13 Solidity 编译器和启用优化进行了测试(200)

测试结果讨论 …

arrays ethereum solidity truffle remix

5
推荐指数
1
解决办法
3007
查看次数

标签 统计

arrays ×1

ethereum ×1

remix ×1

solidity ×1

truffle ×1