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

Reply via email to