[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Would you consider this a bug?



David Kastrup wrote:
> 
> Kevin Ivory <Kevin.Ivory@linmpi.mpg.de> writes:
> 
> > Leaving the bug topic - switching to algorithms
> >
> > David Kastrup wrote:
> > > Unfortunately, translating something like
> > >
> > > a[[5,2,3,0,4,1]] = a
> > > into the equivalent
> > > a = a[[3,5,1,2,4,0]]
> > > is not easy to do in the general case.
> >
> > Actually it is: you need a second call to sort. See example below.
> >
> > > some data that needed to be processed in sorted order, the results
> > > needed to be rearranged in original order.
> > >
> > > Something like
> > > s = sort(a)
> > > a = a[s]
> > > process(a)
> > ; a[s] = a       ; I still think this a dangerous thing to do.
> >   r = sort(s)
> >   a = a[r]
> 
> [netiquette comment cut]
> 
> Second, I refuse to call a significantly time-consuming routine like
> "sort" (O(n log n)) twice without good reason.  Especially when a
> simple workaround like writing
> a[s] = a+0
> will do the trick.
> 

Although I really agree with Kevin in terms of the danger that lies in
using some pecularity of IDL (which makes it hard to understand a
program, too), I must admit that there is a significant difference in
execution speed if you can avoid the second sort command. For a 20000
floating point array ( a=sin(findgen(20000)/360.*!PI) ), I find
execution times of 0.01xx vs 0.06xx secs if once compares (in Eric's
'terminology') a[s]=a[*] with a=a[sort(s)]. Well, this is definitively
not worth risking anything. BUT if you go to 2M elements, the difference
becomes one between 2 secs and 22 secs, which does matter. In any case,
as always: a nice comment in the program would help ;-)

Regards,
Martin.


-------------------------------------------------------------------
Dr. Martin Schultz                   
Department for Engineering&Applied Sciences, Harvard University
109 Pierce Hall, 29 Oxford St., Cambridge, MA-02138, USA

phone: (617)-496-8318
fax  : (617)-495-4551

e-mail: mgs@io.harvard.edu
Internet-homepage: http://www-as.harvard.edu/people/staff/mgs/
-------------------------------------------------------------------