1

Say I have the following Smart Contract:

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.7;

contract Test {
    
    function f1(uint256 a, uint256 b) external pure returns(uint256) {
        uint256 sum = a + b;
        return sum;
    }
    
    function f2(uint256 a, uint256 b) external pure returns(uint256) {
        return a + b;
    }
    
    function f3(uint256 a, uint256 b) external pure returns(uint256 sum) {
        sum = a + b;
    }
}

f1 should be more expensive than f2 and f3, right?

But between f2 and f3, which one is more expensive?

Is f2 cheaper than f3, since it doesn't declare any variables?

1
  • Did you try measuring it? I think the difference if it exists should be minimal when compiling with optimizations, the function dispatch at the beginning also add some extra cost when doing these micro optimizations.
    – Ismael
    Aug 20 at 14:03
0

They are not. The compiler generates identical bytecode for f2 and f3. You can easily verify that by compiling the two versions and comparing the output:

cat <<EOF > contract.sol
contract Test {
    function f(uint256 a, uint256 b) external pure returns(uint256) {
        return a + b;
    }
}
EOF

solc contract.sol --bin --opcodes --metadata-hash none > f2.out

cat <<EOF > contract.sol
contract Test {
    function f(uint256 a, uint256 b) external pure returns(uint256 sum) {
        sum = a + b;
    }
}
EOF

solc contract.sol --bin --opcodes --metadata-hash none > f3.out

diff f2.out f3.out

The output of the diff command is empty, which means that the compiler output matches byte for byte.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.