(HR {~ +)/@:(,&53)
I'm not sure why the sum is a useful index as I don't think its unique, but you
are repeating for each card. Perhaps this is intended?
(HR {~ +/)@:(,&53)
----- Original Message -----
From: Ryan Eckbo <[email protected]>
To: J-programming forum <[email protected]>
Sent: Saturday, January 2, 2016 10:56 PM
Subject: [Jprogramming] Faster array lookup? (poker hand evaluator)
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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm