Skip to main content
added 3 characters in body
Source Link
user19510
  • 28.1k
  • 2
  • 32
  • 49

I can not understand the difference in gas for the following problem. I generated a sha256 hash value and the output i chose to be lowercase hexadecimal. So the output value is hexadecimal string of 64 characters.Then i saved this value to a bytes32 variable inside a smart contract. The smart contract's code is:
pragma solidity ^0.4.23;
contract Project{
bytes32 public value;
function addValue(bytes32 val)public{
value=val;
}
}
Using

pragma solidity ^0.4.23;

contract Project {
    bytes32 public value;

    function addValue(bytes32 val) public {
        value = val;
    }
}

Using remix, I noticed that the operation to add a value cost 43629 for some values and other 43693. So the difference is 64 gas. Can anyone help me understand why this is happening? I tested many values and the difference was 64 gas, was it random? I also tested to store it as string. Then the cost was constant for all inputs, maybe because the values have the same length as i said the inputs values have 64 characters.
 

I can not understand the difference in gas for the following problem. I generated a sha256 hash value and the output i chose to be lowercase hexadecimal. So the output value is hexadecimal string of 64 characters.Then i saved this value to a bytes32 variable inside a smart contract. The smart contract's code is:
pragma solidity ^0.4.23;
contract Project{
bytes32 public value;
function addValue(bytes32 val)public{
value=val;
}
}
Using remix, I noticed that the operation to add a value cost 43629 for some values and other 43693. So the difference is 64 gas. Can anyone help me understand why this is happening? I tested many values and the difference was 64 gas, was it random? I also tested to store it as string. Then the cost was constant for all inputs, maybe because the values have the same length as i said the inputs values have 64 characters.
 

I can not understand the difference in gas for the following problem. I generated a sha256 hash value and the output i chose to be lowercase hexadecimal. So the output value is hexadecimal string of 64 characters.Then i saved this value to a bytes32 variable inside a smart contract. The smart contract's code is:

pragma solidity ^0.4.23;

contract Project {
    bytes32 public value;

    function addValue(bytes32 val) public {
        value = val;
    }
}

Using remix, I noticed that the operation to add a value cost 43629 for some values and other 43693. So the difference is 64 gas. Can anyone help me understand why this is happening? I tested many values and the difference was 64 gas, was it random? I also tested to store it as string. Then the cost was constant for all inputs, maybe because the values have the same length as i said the inputs values have 64 characters.

Source Link
kathi
  • 199
  • 1
  • 12

bytes32 same length hex strings cost different gas why?

I can not understand the difference in gas for the following problem. I generated a sha256 hash value and the output i chose to be lowercase hexadecimal. So the output value is hexadecimal string of 64 characters.Then i saved this value to a bytes32 variable inside a smart contract. The smart contract's code is:
pragma solidity ^0.4.23;
contract Project{
bytes32 public value;
function addValue(bytes32 val)public{
value=val;
}
}
Using remix, I noticed that the operation to add a value cost 43629 for some values and other 43693. So the difference is 64 gas. Can anyone help me understand why this is happening? I tested many values and the difference was 64 gas, was it random? I also tested to store it as string. Then the cost was constant for all inputs, maybe because the values have the same length as i said the inputs values have 64 characters.