我正在尝试通过优化代码来节省汽油。然而,我突然想知道在 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)