Sorry, I omitted the definitions of y and y0 in the 
last benchmark.

   y=: 1e6 [EMAIL PROTECTED] 2e9
   y0=: y+0



----- Original Message -----
From: Roger Hui <[EMAIL PROTECTED]>
Date: Friday, May 25, 2007 9:22 am
Subject: Re: [Jprogramming] index in nub

> > From: Roger Hui <[EMAIL PROTECTED]>
> > Date: Thursday, May 24, 2007 6:17 pm
> > Subject: [Jprogramming] index in nub
> >
> > (~.y)i.y   finds the indices of y in the nub of y .
> > Can you do it faster?
> 
> Spoiler below.
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>   timer=: 6!:2
> 
>   y=: 1e6 [EMAIL PROTECTED] 2e9
>   timer '(~.y)i.y'
> 1.08679
>   timer '(~.i.]) i.~ y'
> 0.437234
> 
>   y=: 1e6 [EMAIL PROTECTED] 0
>   timer '(~.y)i.y'
> 2.62438
>   timer '(~.i.]) i.~ y'
> 1.29647
> 
>   y=: ":&.> 1e6 [EMAIL PROTECTED] 1e5
>   timer '(~.y)i.y'
> 3.00702
>   timer '(~.i.]) i.~ y'
> 1.54582
> 
>   NB. counterexample, not faster
>   y=: 1e6 [EMAIL PROTECTED] 100
>   timer '(~.y)i.y'
> 0.0264034
>   timer '(~.i.]) i.~ y'
> 0.0321949
> 
> Explanation:  The dyad x i. y is not equally fast 
> on all arguments.  Listed from faster to slower:
> 
> - boolean and literal
> - small range integers, where small range means 
> about the size of #x
> - general integers
> - floating point numbers
> etc.
> 
> When y is not one of the fast ones, (~.y)i.y
> requires two slow operations, one for ~.y and
> another for the i. in (~.y)i.y , because ~.y
> and y have the same classification.  In contrast, 
> (~.i.])i.~y (same as (~.t)i.t=.i.~y) has one slow
> operation to do i.~y , producing a list of 
> small-range integers, whence (~.i.]) is a fast
> operation.
> 
> By the way, i.~y , even when "slow", is supported
> by special code, as the following benchmark
> demonstrates:
> 
>   timer 'i.~ y'
> 0.357208
>   timer 'y i. y'
> 0.355987
>   timer 'y i. y0'
> 0.624246
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to