Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dealing poker hands

11 views
Skip to first unread message

pineapple

unread,
Dec 10, 2008, 2:26:54 AM12/10/08
to
10 Texas Holdem players need 25 cards dealt out of a 52 card deck (2
hole cards for each, plus the 5 "public" cards). They will play
100000 rounds, so deal these 25 cards 100000 times.

I'd like to compare 2 things (which may end up having the same
solution, or different solutions):

1. Shortest, most compact code.

2. Fastest performing code.

My interest in this is simply to compare and contrast this with a
poker server I wrote quite a while back in a more "traditional"
language.

For 1, I came up with:

{25?52} ¨ ⍳100000 ( {25?52} each iota 100000 )

Anything else better?

As to 2, besides modifying "iota 100000" to "100000 rho 0" (writing 0s
instead of counting to 100000 should be ever so slightly faster), is
there any material reason not to do the same as in 1? Would an
explicit loop perform better?

RHu...@shaw.ca

unread,
Dec 10, 2008, 2:36:24 AM12/10/08
to
Shorter and faster: 25?¨1e5⍴52

pineapple

unread,
Dec 10, 2008, 2:59:05 AM12/10/08
to
On Dec 10, 2:36 pm, RHui...@Shaw.CA wrote:

> Shorter and faster:  25?¨1e5⍴52

Excellent! APL gurus never cease to amaze me.


As far as "faster," I'm guessing we'd obviously want to suppress any
output to the screen. The only way I know to do that would be by
assigning to a variable. Is that the way to go?

Which begs the question "why have anything to save to a variable in
the first place?" In other words, is there a way to calculate, yet
not store, any results?

RHu...@shaw.ca

unread,
Dec 10, 2008, 3:32:04 AM12/10/08
to
The Dyalog way to kill a result is: {}blah

RHu...@shaw.ca

unread,
Dec 10, 2008, 3:34:39 AM12/10/08
to
J is faster and shorter still: 25?1e5$52

David Liebtag

unread,
Dec 10, 2008, 9:12:24 AM12/10/08
to
> Which begs the question "why have anything to save to a variable in
> the first place?" In other words, is there a way to calculate, yet
> not store, any results?

In APL2 Service Level 13, you can press Ctrl+Enter to calculate the result
and show it in a separate window. When you close the window, the result is
discarded.

David Liebtag
IBM APL products and Services


pineapple

unread,
Dec 10, 2008, 2:45:10 PM12/10/08
to
On Dec 10, 3:34 pm, RHui...@Shaw.CA wrote:
> J is faster and shorter still:  25?1e5$52

How does this version work? There looks to be no "each." Can I
presume that J's deal works with arrays/matrices?

RHu...@shaw.ca

unread,
Dec 10, 2008, 3:24:49 PM12/10/08
to
> > J is faster and shorter still:  25?1e5$52
>
> How does this version work?  There looks to be no "each."  Can I
> presume that J's deal works with arrays/matrices?

In J, the ranks of ? are 0 0. Therefore, in 25?1e5$52
the 25 is paired with each element of 1e5$52.
The overall result is assembled from the individual
results using rules that are the same for all functions.

A function works with arrays of rank equal to or higher
than its specified function rank. Since the ranks of ?
are 0, and all arrays have rank equal to or greater than 0,
that means ? works with arrays of any rank. (? may reject
some arguments, e.g. 'a'?4 5, but such rejection
would not be because of rank.)

Bakul Shah

unread,
Dec 10, 2008, 7:14:29 PM12/10/08
to
RHu...@Shaw.CA wrote:
> J is faster and shorter still: 25?1e5$52

Some interesting comparisons on a macbook pro:

J:
Ts '25?1e5$52'
0.251707 1.73039e7

Q:
q)\t -25?/:100000 52#til 52 / solution similar to the one above
216 / milliseconds
q)\t {-25?52}each til 100000 / similar to the original APL
160
q)\t {-25?52}each 100000#0
131
q)\t do[100000;-25?52] / this is the fastest
95

The last time is slightly faster than a C program
I wrote just to test this, even when compiled with -O9!
Though the C program is faster for larger vectors.

pineapple

unread,
Dec 10, 2008, 7:38:15 PM12/10/08
to
On Dec 11, 7:14 am, Bakul Shah <bakul+use...@bitblocks.com> wrote:
> Some interesting comparisons on a macbook pro:

What were the milliseconds for J, and can you also do benchmarks for
APL?

(Incidentally, if someone could tell me how to do timing in APL - say
Dyalog, that would be great)

Bakul Shah

unread,
Dec 10, 2008, 9:10:31 PM12/10/08
to
pineapple wrote:
> On Dec 11, 7:14 am, Bakul Shah <bakul+use...@bitblocks.com> wrote:
>> Some interesting comparisons on a macbook pro:
>
> What were the milliseconds for J, and can you also do benchmarks for
> APL?

For J it was 0.251707 seconds (252 ms) -- I believe the Ts function
computes time and space used by an expression. I don't have any APL
on this machine. The interesting part was that even in something
simple as this, performance depended on the method chosen. But beyond
that this comparison doesn't mean anything.

Phil Last

unread,
Dec 11, 2008, 3:28:44 AM12/11/08
to

time←{
⍺←{⍵}
0.001×÷/2⍴⌽1{⍵}∘({⍵}∘(⍺∘⍺⍺)∘⍵){⍺,⊃∇/⍺{(1000<⍵)↓(2×⍺)⍵}⍺⍺{⍬⍴2↓⎕AI-
(⍺⍺/⍳1+⍵){⍵}⎕AI}⍺}1
⍝ time function
⍝ seconds per execution averaged over at least one second
}
This is an operator. If you want to time the expression, say, 2 ⍟
1+⍳10
you code the operator between the function and its right argument so:

2 ⍟ time 1+⍳10
0.0000053634643554688

It returns the average time per iteration having done it enough times
to take at least one second in total.

Phil Last

unread,
Dec 11, 2008, 3:30:43 AM12/11/08
to

I appear to have hit the wrong reply button. was meant to answer the
previous post!

Jane Sullivan

unread,
Dec 11, 2008, 8:24:03 AM12/11/08
to
In message
<2ca77f22-4e2c-46d9...@a26g2000prf.googlegroups.com>,
pineapple <pineapp...@yahoo.com> writes

Look at []MONITOR in Dyalog.
--
Jane Sullivan

0 new messages