I transferred ETH 3 times to a smart contract which needs 48734 actual gas units to execute.
It took 68932 units for 1st tx and 48734 for remaining 2. Why it took more gas for the first tx than latter 2?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.Sign up to join this community
Most of that is probably storage. One of the most expensive operations, at 20k gas, is allocating a new storage slot. So when a smart contract sets a storage slot from zero to non-zero, the sender of the tx is charged 20k gas. Similarly, when a storage slot is set from a non-zero to a non-zero (updating an already allocated storage slot), thea sender of the tx is charged 5k. This often means that for a lot of systems the user is charged more for a first time invocation, especially with token contracts because it has to initialize the users balance.
Aside from that, normal branching can often cause a few hundred to several thousand in difference. Also, a common difference is in the tx data. Tx data is charged 64 gas for non-zero bytes and only 4 gas for zero bytes, so if two people call the same function but my parameters have more zero bytes, then it's cheaper for me.
More information on opcode pricing can be found in the yellowpaper on pages 20 and 21.