Created
February 4, 2021 06:58
-
-
Save richpsharp/94a65b0b01c6fffcb47ec00c999f7938 to your computer and use it in GitHub Desktop.
Time different kinds of slicing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import numpy | |
def mask_op(array_a, array_b, nodata): | |
result = numpy.full(array_a.shape, nodata, dtype=numpy.float) | |
valid_mask = numpy.ones(array_a.shape, dtype=numpy.bool) | |
valid_mask &= (array_a != nodata) & (array_b != nodata) | |
result[valid_mask] = array_a[valid_mask]+array_b[valid_mask] | |
return result | |
def mask_op_noneslice(array_a, array_b, nodata): | |
result = numpy.empty(array_a.shape, dtype=numpy.float) | |
result[slice(None)] = nodata | |
valid_mask = numpy.empty(array_a.shape, dtype=numpy.bool) | |
valid_mask[slice(None)] = True | |
valid_mask &= (array_a != nodata) & (array_b != nodata) | |
result[valid_mask] = array_a[valid_mask]+array_b[valid_mask] | |
return result | |
op_time = 0 | |
noneslice_op_time = 0 | |
nodata = -1 | |
N = 1000 | |
D = 2**8 | |
for _ in range(N): | |
array_a = numpy.random.random((D, D)) | |
array_b = numpy.random.random((D, D)) | |
nodata_a_mask = numpy.random.random((D, D)) > 0.5 | |
nodata_b_mask = numpy.random.random((D, D)) > 0.5 | |
array_a[nodata_a_mask] = nodata | |
array_b[nodata_b_mask] = nodata | |
start_time = time.time() | |
result_base = mask_op(array_a, array_b, nodata) | |
op_time += time.time() - start_time | |
start_time = time.time() | |
result_noneslice = mask_op_noneslice(array_a, array_b, nodata) | |
noneslice_op_time += time.time() - start_time | |
print(f'D: {D}') | |
print(f'op_time: {op_time}\nnoneslice_op_time: {noneslice_op_time}') | |
print(f'noneslice performance increase: {op_time/noneslice_op_time*100:.2f}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment