I've implemented the twoplustwo 7 card poker hand evaluator[1] in J but it's
slower than I expected.  The evaluator works by accepting an array of 7
integers between 1 and 52, representing a 7 card hand, and uses them as indices in a lookup table, which returns the best 5 card hand's rank value. Here's the
C code that does this (HR is the lookup table):

int LookupHand(int* pCards)
{
    int p = HR[53 + *pCards++];
    p = HR[p + *pCards++];
    p = HR[p + *pCards++];
    p = HR[p + *pCards++];
    p = HR[p + *pCards++];
    p = HR[p + *pCards++];
        return HR[p + *pCards++];
}

My J version is:

hr=: (HR {~ +)/@:(,&53)      NB. hr 1 5 9 12 20 25 42

This is one of the fastest evaluators out there, for example a javascript version can run at 20 million evaluations per second. My J version takes 30 seconds
to evaluate just 10 million hands:

hands=. 53,.~ >: ? 1e7 7 $ 52 NB. 10 million hands, with initial index 53 added
6!:2 '(HR {~ +)/"1 hands'  NB. 32.14 sec on a 2.8GHz Mac

I wonder why it's slow and if there's a way to make it faster?


[1] https://github.com/christophschmalhofer/poker/blob/master/XPokerEval/XPokerEval.TwoPlusTwo.Test/test.cpp
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to