Let’s understand this in a mathematical manner.

The **Transfer **function commits 2 writes to storage. One while updating the balance of the **from **address and one while updating the balance of the **to **address.

Depending on old and new values, gas costs vary significantly.

Coming to your transactions now,

In the first transaction, **to **address has zero initial balance but the **from **address has some tokens left after the transaction. We know that changing a zero to non-zero in storage costs us 20,000 gas. This is basically the cost of updating the **to **address. However, changing a non-zero value to another non-zero value costs 5,000 gas. And this is the cost of updating the **from **address. Therefore the total cost of both these instructions sums up to be **25,000** gas.

In the second transaction, **to **address has a positive initial balance and this balance increases. But the balance in the **from **address is completely taken up by the transaction. The fact to consider here is that when we update the **from **address, a non-zero value is changed to zero and therefore you get a gas refund. Cost is 5,000 but after considering the refund of 15000 gas the total becomes -10000 gas. Therefore the total of both transactions comes down to -5000 gas.

Finally, the overall difference is 25000 - -5000=30,000 gas.