I am trying find the gas used by the transaction when a method is clicked in the remix solidity IDE. my code is as below. Value I am getting in the gasUsed variable is different to the value that is being shown on the etherscan explorer for this transaction. It would be helpful if someone helps me in correcting my code.
pragma solidity ^0.4.22;
contract id{
uint public id;
uint public senderValue;
uint256 public gasUsed;
constructor() public {
senderValue= msg.sender;
}
function setId(uint _id) public {
uint256 gasInitial = gasleft();
id= _id;
setGasUsed(gasInitial - gasleft());
}
function setGasUsed(uint256 _gasUsed) private {
gasUsed = _gasUsed;
}
}