Compiling often helps. When I compile looping, scalar-dominated
APL code, I generally get about a factor of 1000X speedup.
You should get the same sort of speedup with compiled J.
[No, I do not have a J compiler, but will be happy to create one
if funding were to materialize.]
However, you mentioned that you were "looping due to memory
constraints...". Is it possible that a different algorithm would let
you do less looping? E.g., looping over 1e4 elements at once,
or using a different approach, such as dynamic programming?
Bob
On 15-02-03 02:53 PM, Joe Bogner wrote:
I'm playing around with a coding challenge that I'm solving with a
loop that needs to execute over 200 million times.
Setting aside whether that's a smart approach, what is the quickest
looping construct in J? I realize that J is not optimized for this
style of programming, but I'm still curious if it can be done
efficiently.
The quickest I've found takes about 7 seconds to iterate 100 million
times, whereas the same loop in C takes 0.03 seconds.
6!:2 '(] [ ])^:] 100e6'
7.00952
The train is taking up some time, but eventually I will need it
No train - 2.7 seconds
6!:2 ']^:] 100e6'
2.70656
Explicit:
loop=: 3 : 0
ctr=:0
while. 1 do.
ctr=:>:ctr
if. y < ctr do.
smoutput 'done'
return.
end.
end.
)
6!:2 'loop 100e6'
122.48
Clearly explicit isn't the way to go
Back to the tacit:
log=:smoutput bind ]
([: <: [: 0&{:: ] ; log )^:] 10
10
9
8
7
6
5
4
3
2
1
0
I would replace log with my function that uses the counter value
That's pretty slow though on 100 million iterations - 88 seconds
6!:2 '([: <: [: 0&{:: ] ; ] )^:] 100e6'
88.3644
Let's try a new log
log=: ] <:@:[ (smoutput bind ])
log 55
55
54
That looks like it could work. Let's put a dummy in for now
log=: ] <:@:[ ]
6!:2 'log^:] 100e6'
40.9542
Still 40 seconds... Any ideas on how to speed up iteration like this?
Sidenote: I'm looping due to memory constraints placed on the coding challenge
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
--
Robert Bernecky
Snake Island Research Inc
18 Fifth Street
Ward's Island
Toronto, Ontario M5J 2B9
[email protected]
tel: +1 416 203 0854
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm