2

I was testing this code using Remix. I was wondering why the execution cost of the function (in gas) depends on the input x. The cost seems to increase in multiples of 12 as the value of x increases. I haven't found a pattern.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.4; 
contract Example{
    function test (uint x) external pure returns(bool z) {
        if(x > 0)
          z=true;
        else
          z=false;
    }
}
Flavia Giammarino
  • 7,987
  • 11
  • 30
  • 40
vipin
  • 1,610
  • 3
  • 16
  • 27

2 Answers2

3

https://github.com/wolflo/evm-opcodes/blob/main/gas.md#a0-0-intrinsic-gas

The bytes sent to the contract indeed decides the gas cost as can be seen from the link. gas_cost += 4 * bytes_zero: gas added to base cost for every zero byte of memory data gas_cost += 16 * bytes_nonzero: gas added to base cost for every nonzero byte of memory data

so if you send 0x0001 or 0x0010 it will cost the same amount of gas. But if you send 0x0011 it will cost 12(16-4) gas more than the previous case.

ElKu
  • 46
  • 4
1

Gas is charged in three scenarios

Yilmaz
  • 35,338
  • 10
  • 157
  • 202