0

So, which would be gas efficient?

Reading a storage variable in a state-changing function?

OR

Reading from a pure function in a state-changing function?

Example:

contract Test {
    uint256 public a = 10;
    
    uint256 public b = 1;
    
    function aPure() public pure returns(uint256) {
        return 10;
    }
    
    function aFunc() public {
        b = a;
    }
    
    function aPureFunc() public {
        b = aPure();
    }
}

In the above, will aFunc() have lower gas cost or aPureFunc()?

0

So, a simple way I used to check the cost is in Remix IDE, with Gas Profiler. And the results were:

aFunc()

Transaction cost: 23096

Execution cost: 1824

aPureFunc()

Transaction cost: 22265

Execution cost: 993

So, it turns out, reading from the pure function is gas efficient compared to reading from storage if there are no calculations being done.

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.