2

1-Does execution of same function in a contract uses different amount of gas each time it's executed?
2-Also if same function (for eg, transfer() function) is written in two different contracts, will it uses different amount of gas?
3-Mainly on what factors gas consumed for a transaction depends?

2

1 - yes

2 - yes

3 - The answer to the first two questions is yes because the gas consumed by a function depends on the number of computations done in that function. And that could depend on the input. For example you could have:

function loop(uint number){

while(number!=0){
number--;
      }

}

It is pretty obvious that if your input is 1000 there will be a lot more computations done thane when the input is 1.

  • Thanks. I was concerned about if I have same functions in 2 different contracts and I pass same input parameters so that everything remains same. Can the gas consumed differ? I mean if gas used is contract dependent or time depended? I know of course it depends on amount of calculation performed. But if amount of calculation performed is same , still the gas used may differ or not? – Prashant Prabhakar Singh Aug 1 '16 at 8:58
  • If they have the same input and the same code then they should consume the same amount of gas – dragosb Aug 1 '16 at 9:02
  • I have created a crypto-currency by following this. While exmining the gas used for each transaction, I noticed that functions like transfer(..) , transferFrom(...) consume same amount of gas irrespective of amount transferred or account which is as per expectation. But every time I call approveAndCall(..) the gas used varies. Also the variation in gas used is around 33% which is quite large. Any explanations why this is happening? – Prashant Prabhakar Singh Aug 4 '16 at 11:07
  • in approveAndCall(..) what you get different amounts even if you have the same parameters in two different calls? if you do vary some parameters...which one ? – dragosb Aug 4 '16 at 13:24
  • Even for same parameters I am getting different amount of gas consumed. But I think even for different calls it should be same. For eg, whether I approve 100 coins to a account or 500, the amount of calculation performed each time should not differ. Like in transfer whether I transfer 50 coins or 500, the gas used is same. So same should be the case with approveAndCall(..). Other functions such as mintToken(...) changeReward() also use variable gas in different calls. – Prashant Prabhakar Singh Aug 5 '16 at 4:53
1

When I was trying to call transfer function with same parameters on OMG contract, I saw first call is consuming more gas, but following calls consume a lesser amount than the first but the same amount in all.

Finally I found that as at first call, the destination address didn't exists in contract, adding that to the address list resulted in different execution routine with more gas. But after first transfer, following calls all had same gas amount because of same execution routine.

These are transaction hashes you can check for more information:

First transaction with higher gas amount: 0xef0bd9720c7e8661e98f36ed5327f94ce3ce8f5c475027bb8c276c2004dbaef2

Following transactions with same lower gas amount:

0x7bd5da6f2076fd75d5204c7517d3f487c3b26da0b2a28d905f51bbc37acf2e8a 0xe22f976419c24424459bcceb0f4ed97fa6e257146213602c7cf792b45c6d2c30 0x8166f85f4c1542784f04e1e8579df8ff23abc261725001baf5984939a2262fcf

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.