I think you were asking, not whether you can optimize this form
(which you can, as others explained), but whether you can
make a sequence of verbs share one loop as you can in C.

[as in:

  for(total = 0, i = 0;i<len;++i)total += (4 < pi[i]);

]


The real answer is, You must stop thinking like that.  Putting
things into one loop is not a good idea in J.  One reason is that
just adding one scalar to another requires finding the type of
the scalar & deciding what to do, & you don't want to do that
for each item; rather, you want to work on arrays in toto.  The
more important reason is that you want to train yourself to stop
thinking about scalars and start thinking about the whole problem,
which will lead you to frame your thought in terms of arrays.

Generally you are best off when you let J work on the biggest possible
pieces, which means more loops each looping over more data.



If you were doing

- x >. y  NB. negative of the larger of x and y

You could write it as

x -@:>. y
  for(i = 0;i<len;++i)t = MAX(x[i],y[i]);
  for(i = 0;i<len;++i)r = -t[i];  

which loops twice, or

x -@>. y
  for(i = 0;i<len;++i)result = - MAX(x[i],y[i]);

which loops once.  The loops-once version is much slower.  

If you find yourself worrying abut cache footprint and the like,
you gotta get over that.  J uses a lot of intermediate memory but
it is nonetheless quite fast, and not a memory hog overall.

Henry Rich

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Terrence Brannon
> Sent: Wednesday, March 28, 2007 3:57 PM
> To: General forum
> Subject: [Jgeneral] optimization of array passes?
> 
> Given this verb:
> 
>    +/ 4 < 3 1 4 1 5 9
> 
> does it go through the list once with the verb < and then 
> again with the verb +/
> or can it do it in one pass like I could in C?
> ----------------------------------------------------------------------
> For information about J forums see 
> http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to