A pure R poker hand evaluator

[This article was first published on Modern Toolmaking, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

There’s already a lot of great posts out there about poker hand evaluators, so I’ll keep this short.  Kenneth J. Shackleton recently released a very slick 5-card and 7-card poker hand evaluator called SpecialK.  This evaluator is licensed under GPL 3, and is described in detail in 2 blog posts: part 1 and part 2.  Since the provided code is open source, I felt free to hack around with it a bit, and ported the python source to R.

You can download my code from github, and save it to ~/SpecialK/R.  Run the following script to initialize the evaluator and test it out.  Higher numbers=better hands.

My R code is almost identical to the python source, as all I did was change python classes to R lists and make the lists 1-indexed (R) rather than 0-indexed (python).  I also used the wonderful bitops package for bitwise operations. Obviously this code should be vectorized, but I don’t have time to do that right now.  I also used the “compiler” package to speed things up somewhat, but the “SevenEval” file still takes a long time to load.

Right now, I’m getting about 18,000 hands per second on my core i5 laptop, which is pretty crappy compared to the 250 million hands per second Shackleton reports for the C++ version.  Vectorization should further increase performance, but I don’t think pure R is ever going to approach C++ in terms of raw speed.  While this was a fun port to make, I think this code is an obvious candidate for a re-write using the Rcpp package.

I’m interested to see how far the pure R code can be optimized.

To leave a comment for the author, please follow the link and comment on their blog: Modern Toolmaking.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)