There are four ways to increment and decrement a variable in Solidity.
//SPDX-License-Identifier:MIT
pragma solidity ^0.8.3;
contract test1{
uint256 public number;
// transaction cost: 43816
function increment() public returns (uint256){
number += 1;
return number;
}
}
contract test2{
uint256 public number;
// transaction cost: 43803
function increment() public returns (uint256){
number = number + 1;
return number;
}
}
contract test3{
uint256 public number;
// transaction cost: 43653
function increment() public returns (uint256){
return number++;
}
}
contract test4{
uint256 public number;
// transaction cost: 43647
function increment() public returns (uint256){
return ++number;
}
}
//SPDX-License-Identifier:MIT
pragma solidity ^0.8.3;
contract test1{
uint256 public number = 1;
// transaction cost: 21894
function decrement() public returns (uint256){
number -= 1;
return number;
}
}
contract test2{
uint256 public number = 1;
// transaction cost: 21881
function decrement() public returns (uint256){
number = number - 1;
return number;
}
}
contract test3{
uint256 public number = 1;
// transaction cost: 21731
function decrement() public returns (uint256){
return number--;
}
}
contract test4{
uint256 public number = 1;
// transaction cost: 21725
function decrement() public returns (uint256){
return --number;
}
}