2

I have the following contracts:

pragma solidity ^0.4.17;

contract one{
    uint firstNum;
    uint secondNum;
    uint thirdNum;
    function getNumber(uint _num, uint _num2,uint _num3) public{
       firstNum = _num;
       secondNum = _num2;
       thirdNum = _num3;
    }
}

contract two{
    uint firstNum;
    uint secondNum;
    uint thirdNum;
    function getNumber(uint[3] numValues) public{
       firstNum = numValues[0];
       secondNum =numValues[1];
       thirdNum = numValues[2];
    }
}

Literally the only difference is the input function. Now for the costs:

Contract one creation:

  • Transaction cost: 102814 gas.
  • Execution cost: 37682 gas.

and function call 1,2,3 as input:

  • Transaction cost: 82106 gas.
  • Execution cost: 60258 gas.

Contract two creation:

  • Transaction cost: 121264 gas.
  • Execution cost: 51500 gas.

and function call [1,2,3] as input:

  • Transaction cost: 82296 gas.
  • Execution cost: 60448 gas.

So a few questions...why is it more expensive? and now why do contracts like 0x use arrays versus just entering them separately?

1

Your contract is very simple and execution cost is dominated by storage costs.

Storing each uint in your contract's storage use 20000 gas. Your contract has three of them storing them will use 60000 gas.

Arrays in solidity needs some extra processing, see Diving Into The Ethereum VM Part 3 — The Hidden Costs of Arrays (to check for out of bounds condition). So functions using them will be longer.

This contribute to the contracts to be larger and more expensive to deploy. But deployment is done only once so it is not very serious issue.

One of the possible reasons that 0x use arrays as parameters is that using too many parameters in a function cause a compiler error "Stack too deep".

  • Thanks for the feedback. the 'stack too deep' angle makes sense. – thefett Feb 11 '18 at 18:02

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.