Forgot to mention that I also have one other optimisation up my sleeve: instead of fully analysing each hand I could only do that when I need it i.e. when the hand type is the same, so I could just return “flush” and only if your opponent also has a flush would I need to say “flush ace high” or whatever. This provision of the detail is actually quite time-consuming, so it may be a significant improvement to avoid it in most cases.
Current timings (without the above optimisation) on a P4 2.4GHz machine:
[andy@andy-p4-linux src]$ time ./poker 5 # Analyse all possible 5-card hands Straight flush: 0.001539% Four of a kind: 0.024010% Full house: 0.144058% Flush: 0.196540% Straight: 0.392465% Three of a kind: 2.112845% Pair of pairs: 4.753902% Pair: 42.256903% High card: 50.117739% real 0m4.058s user 0m3.805s sys 0m0.002s [andy@andy-p4-linux src]$ time ./poker 7 # Analyse all possible 7-card hands Straight flush: 0.016811% Four of a kind: 0.168067% Full house: 2.555057% Flush: 3.039766% Straight: 4.475084% Three of a kind: 4.873941% Pair of pairs: 23.512489% Pair: 43.946865% High card: 17.411920% real 3m43.479s user 3m42.963s sys 0m0.047s
This is nice, because there are 51 times as many 7-card hands as 5-card ones, and the analysis only takes 55 times as long even though it’s more complex to analyse larger hands, so it means my analysis scales pretty well with the number of cards.