From 72772ae0ddf763a0edadc9981bc423e79bff4a49 Mon Sep 17 00:00:00 2001 From: timcera Date: Tue, 20 May 2014 20:06:19 -0400 Subject: [PATCH] Numpy boolean slicing rather than multiplying The multiplication by the boolean is computationally expensive compared to using boolean indexing. Speeds it up a bunch. --- python/gdal-performance/classification_numpy_arrays.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/gdal-performance/classification_numpy_arrays.py b/python/gdal-performance/classification_numpy_arrays.py index bb6fed3..c533fd6 100644 --- a/python/gdal-performance/classification_numpy_arrays.py +++ b/python/gdal-performance/classification_numpy_arrays.py @@ -26,9 +26,9 @@ r = zeros((ysize, xsize), numpy.float) for i in range(len(classification_values) - 1): - r = r + classification_output_values[i] * logical_and(data >= classification_values[i], data < classification_values[i + 1]) + r[logical_and(data >= classification_values[i], data < classification_values[i + 1])] = classification_output_values[i] -r = r + classification_output_values[i] * (data >= classification_values[i + 1]) +r[(data >= classification_values[i + 1])] = classification_output_values[i] format = "GTiff" driver = gdal.GetDriverByName( format )