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?
> 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?
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
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.)
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.
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)
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.
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.
I appear to have hit the wrong reply button. was meant to answer the
previous post!
Look at []MONITOR in Dyalog.
--
Jane Sullivan