Welcome to the forum @BTim… I hope you’re enjoy the course! 
Your solution is correct, and really well done for considering the gas consumption, and attempting to calculate the difference in gas consumption between the difference solutions 
As you have discovered, Remix generates the gas consumption for each transaction (including deployment) within the transaction receipts in the Remix console. However, I’m aware that they may not be entirely accurate. The gas consumption in Remix is also split between transaction and execution costs, and I still haven’t entirely got to the bottom of how this split is arrived at in Remix, and whether there is any double counting or other aspects that need to be taken into consideration. It’s also possible to click on “Debug” next to a transaction receipt, which will take you to the Debugger in the panel on the left. Here you can click through each of the separate operations performed in that transaction and see the gas consumed for each one. I’ve used that before to identify the gas consumption for each individual, low-level operation performed for a specific chunk of code. By adding those up, I’ve tried to arrive at the total gas consumed for alternative chunks of code, in order to compare how much gas they use. But, as I’m sure you can imagine, this gets quite time consuming!
A more straight forward way to arrive at the gas consumption is to deploy the contract using Truffle and Ganache, and then perform various transactions using the Truffle console. You will learn how to use these tools in the 201 course, which follows this one.
In fact, your solution should consume more gas, because storing the User
instance temporarily in memory creates a copy of the data. However, when we assign users[id]
to a local storage
variable, we create a local “pointer”, which points to (references) the User
instance in the mapping, but doesn’t create a copy of it. Any value assigned to the local “pointer” variable is effectively updating a specific instance in the mapping directly. To put it another way, the local storage
variable user
creates a temporary “bridge” to the mapping during execution of the function, so that the new balance
can be assigned to a specific user’s balance property in the mapping, before this “bridge” is lost when the function finishes executing.
So, expecting a different result to yours, I’ve used Truffle and Ganache to test your assignment solution and the 2 others demonstrated in the solution video, and here are my findings:
Results:
// Test 1 - Code in updateBalance function body (your solution)
User memory user = users[id];
user.balance = balance;
users[id] = user;
/* Gas used to deploy contract: 170005
Gas used to execute updateBalance function: 29406 */
// Test 2 - Code in updateBalance function body
User storage user = users[id];
user.balance = balance;
/* Gas used to deploy contract: 147163
Gas used to execute updateBalance function: 26712 */
// Test 3 - Code in updateBalance function body
users[id].balance = balance;
/* Gas used to deploy contract: 145879
Gas used to execute updateBalance function: 26699 */
/* Gas used to execute addUser function to create new User instance
is the same in each (function code doesn't change): 61812
This is in line with what I would expect. Your solution consumes approx. 2700 more units of gas than the other two. The two from the solution video consume more or less the same amount of gas, which again is to be expected, because despite the code being different, the actual operations being performed are the same: they both update the mapping directly. This is cheaper, because we are not creating any additional storage — just updating the existing storage. However, it’s more expensive to include the extra step of updating a local copy of the User
instance (stored in memory) with the new balance, before then assigning this updated copy to the mapping.
The following article is an easy read about gas consumption, which you may find helpful…
https://medium.com/coinmonks/understanding-gas-in-ethereum-53ad816f79ae
Just let me know if you have any questions 