This is the development site for Hands-On Programming with R, a book that teaches how to program in R, with hands-on examples. Read the book at https://rstudio-education.github.io/hopr.
Other
254
stars
331
forks
source link
Faster and simpler vectorised prize calculation #67
I took the advice in the book to implement a faster vectorised score-calculation function using rowSums and vectorised selective updates. The method of decomposing the problem differs from that in the book. Namely, wild diamonds are handled by adding the diamond count to each check, as, per the problem description, the diamonds can be thought of as any other symbol (an exception is made for the cherry cases).
The resulting function is about 3x shorter and, on my machine, about 3x faster than the book's example solution. In my opinion, it's also significantly simpler in the way it handles wild symbols, and I think the method could also easily be generalised to new similar sets of rules without significant alteration. A branch-based, non-vectorised version is also possible, which is of similar complexity and speed to the book's example (but it doesn't serve as a good demonstration of vector lookups).
I tested both versions and they give identical output to the book's code for all combinations of symbols. An rmarkdown script for testing and for profiling with microbenchmark is attached:
Post the alternative method here for others to learn from if interested
Contribute the simpler method in case the author would like to adapt it for use in future versions of the book
Invite others to further improve the efficiency of the method and share their approaches and results
My thanks to the author and all contributors for their work on making an excellent open source book that helped me efficiently learn the fundamentals of R.
I took the advice in the book to implement a faster vectorised score-calculation function using
rowSums
and vectorised selective updates. The method of decomposing the problem differs from that in the book. Namely, wild diamonds are handled by adding the diamond count to each check, as, per the problem description, the diamonds can be thought of as any other symbol (an exception is made for the cherry cases).The resulting function is about 3x shorter and, on my machine, about 3x faster than the book's example solution. In my opinion, it's also significantly simpler in the way it handles wild symbols, and I think the method could also easily be generalised to new similar sets of rules without significant alteration. A branch-based, non-vectorised version is also possible, which is of similar complexity and speed to the book's example (but it doesn't serve as a good demonstration of vector lookups).
I tested both versions and they give identical output to the book's code for all combinations of symbols. An rmarkdown script for testing and for profiling with microbenchmark is attached:
This issue serves three purposes
My thanks to the author and all contributors for their work on making an excellent open source book that helped me efficiently learn the fundamentals of R.