# [Numpy-discussion] Multiple Boolean Operations

Andrea Gavana andrea.gavana@gmail....
Thu May 22 14:16:23 CDT 2008

```Hi All,

On Thu, May 22, 2008 at 7:46 PM, Robert Kern  wrote:
> On Thu, May 22, 2008 at 12:26 PM, Stéfan van der Walt <stefan@sun.ac.za> wrote:
>> Just to clarify things in my mind: is VTK *that* slow?  I find that
>> surprising, since it is written in C or C++.
>
> Performance can depend more on the design of the code than the
> implementation language. There are several places in VTK which are
> slower than they strictly could be because VTK exposes data primarily
> through abstract interfaces and only sometimes expose underlying data
> structure for faster processing. Quite sensibly, they implement the
> general form first.

Yes, Robert is perfectly right. VTK is quite handy in most of the
situations, but in this case I had to recursively apply 3 thresholds
(each one for X, Y and Z respectively) and the threshold construction
(initialization) and its execution were much slower than my (sloppy)
numpy result. Compared to the solution Francesc posted, the VTK
approach simply disappears.
By the way, about the solution Francesc posted:

xyzReq = (xCent >= xMin) & (xCent <= xMax) &  \
(yCent >= yMin) & (yCent <= yMax) &  \
(zCent >= zMin) & (zCent <= zMax)

xyzReq = numpy.nonzero(xyzReq)[0]

Do you think is there any chance that a C extension (or something
similar) could be faster? Or something else using weave? I understand
that this solution is already highly optimized as it uses the power of
numpy with the logic operations in Python, but I was wondering if I
can make it any faster: on my PC, the algorithm runs in 0.01 seconds,
more or less, for 150,000 cells, but today I encountered a case in
which I had 10800 sub-grids... 10800*0.01 is close to 2 minutes :-(
Otherwise, I will try and implement it in Fortran and wrap it with
f2py, assuming I am able to do it correctly and the overhead of
calling an external extension is not killing the execution time.

Thank you very much for your sugestions.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/
```