2

I have this contract with this functions, why each use different gas amount?

pragma solidity 0.4.24;

contract Foo {
  //transaction cost: 21471 gas 
  //execution cost: 199 gas
  //total cost: 21670 gas
  function withoutNewS() external {
    uint256[3] storage data;
  }
  //transaction cost: 21427 gas 
  //execution cost: 155 gas
  //total cost: 21582 gas
  function withoutNew() external {
    uint256[3] data;
  }
  //transaction cost: 21533 gas 
  //execution cost: 261 gas
  //total cost: 21794 gas
  function withoutNewM() external {
    uint256[3] memory data;
  }
  //transaction cost: 21607 gas 
  //execution cost: 335 gas
  //total cost: 21942 gas
  function withNewM() external {
    uint256[] memory data = new uint256;
  }
}

1 Answer 1

0

This is because you are leveraging different memory locations of the EVM (memory and storage have very different meanings and implications, as you can see here for example). These different elements have different purposes and you should always strive for the least gas-consuming one which is still fit for your purpose, as EVM rewards carefully optimised code with less costs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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