[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Zero vector detection in IDL
Frank Morgan wrote:
> Given a big byte or integer array (actually a 1-D vector), is there a
> fast IDL way to check whether any non-zero elements exist?
> Something like "if (total(x) EQ 0)..." or a where() construct will work,
> but scans the whole vector when the first non-zero element is enough to
> answer the question. max(x) is even worse.
> On the other hand:
> for i=0,n-1 do begin
> if (x(i) NE 0) then begin
> (it's not all zero)
> goto, BREAK
> will stop early if possible, but looping a
> conditional is maybe not the
> fastest structure in IDL.
> An internal command that implements the loop concept would be what I'm
> looking for I think but I don't know if it exists.
> Any better ideas?
I was curious just how slow loops are. I had to revert to IDL 5.1 on my
linux machine to get systime(1) doesn't work... hint hint. Anyway, I
investigated three methods:
1. the loop above
2. (where(a ne 0.0d)) ne -1
3. a wrapped call_external to a C program similar to the above loop
I used a double vector of length 1 million:
1. Average Time: 0.9944s
2. Average Time: 0.1172s
3. Average Time: 0.04447s
Two things to notice: IDL loops are *very* slow, and where isn't *too*
bad for having had to search the full vector. The external program was
just a little snippet like:
arr = (double*)argv;
if(arr[i]!=0.0) return 1;
and I used a wrapper routine like:
This only really wants doubles, but it shouldn't be hard to add the type
also, and cast the array pointer accordingly. See the external examples
for details on compiling for your system.
J.D. Smith |*| WORK: (607) 255-5842
Cornell University Dept. of Astronomy |*| (607) 255-6263
304 Space Sciences Bldg. |*| FAX: (607) 255-5875
Ithaca, NY 14853 |*|