1. Monopoly recap
We'll use this final video in the chapter to see how our small changes to the
code, affect the overall run time. Using profvis we were able to show that the move square function was the bottleneck. The main issue within this function was creating a data frame.
2. Data frames vs. matrices
By changing the data frame, to a matrix, the total run time of the entire Monopoly simulation was reduced from two seconds to around half a second.
This is because, compared to a matrix, creating a data frame is a time consuming process. Since we are calling this function ten thousand times, this small inefficiency quickly adds up.
3. apply vs. rowSums
The next slowest line was using apply to calculate row sums. This piece of code was needed to calculate how many spaces a player would move after rolling two dice.
Changing from apply to the specialized function rowSums, reduced the run time from half a second to 0-point-16 seconds. The final optimization was tweaking the if statements.
4. & vs. &&
When we use the double ampersand, the second argument is only evaluated if the first argument is true. As you would imagine, this gives a tiny speed-up - only 0-point-01 seconds. Overall, we have achieved a ten fold speed-up
5. Overview
by changing four lines of code. Not bad for a days work.
6. Let's practice!