1

Which type is more expensive for a smart contract? If I just have static text messages to display which type is better?

  • bytes32 uses less gas because it fits in a single word of the EVM. – Malone Nov 13 '17 at 11:26
3

Bytes32 is going to be better in gas if its not changing. If you want to play around with it, I built a little fiddle of it https://ethfiddle.com/70ipaEIFdk

Byte used 21465 gas

String used 21897 gas

pragma solidity ^0.4.18;

contract SampleOverflow {
  string constant statictext = "HelloStackOverFlow";
  bytes32 constant byteText = "HelloStackOverFlow";
  function  getString() payable public  returns(string){
    return statictext;
  }

  function  getByte() payable public returns(bytes32){
    return byteText;
  }
}

Get Byte GetByte

Get String

enter image description here https://ethfiddle.com/70ipaEIFdk

0

You could use Remix and test the folowing contract

pragma solidity ^0.4.8;


contract TestContract  {

bytes32 byt;
string str;

function setByte(bytes32 value) public {
   byt = value;
}


function setStr(string value) public {
   str = value;
}

}

For the string, this is the output:

transaction cost 42975 gas
execution cost 20871 gas

But I have no idea how to pass bytes32 via Remix

0

If I just have static text messages to display which type is better?

Clearly if you have static text message,bytes32 is better in terms of Gas.

pragma solidity ^0.4.0;

contract Sample {
     string constant statictext = "Hello";
     bytes32 constant statictext1 = "Hello";
    function  getStatictextasString() constant public  returns(string){
        return statictext;
    }

     function  getStatictextasBytes() constant public returns(bytes32){
        return statictext1;
    }
}

From executing the sample contract in remix,the gas cost is less when you use bytes32 compared to string.

bytes32 :21506 String :21934

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