Skip to content

Instantly share code, notes, and snippets.

@chapmanjacobd
Last active March 9, 2023 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chapmanjacobd/c70c3c8577e532fafade4b437fc94a69 to your computer and use it in GitHub Desktop.
Save chapmanjacobd/c70c3c8577e532fafade4b437fc94a69 to your computer and use it in GitHub Desktop.
Actual speed impact of reading mismatched blocks
import math
import osgeo.gdal as gdal
import timeit
file_path = "1677269839.tif"
ds = gdal.Open(file_path)
file_block_size = ds.GetRasterBand(1).GetBlockSize()
xoff = 0
yoff = 10000
band = ds.GetRasterBand(1)
mismatched_size = 256
iterations = 1000
def num_blocks(x_block_size, y_block_size):
num_blocks_x = math.ceil(ds.RasterXSize / x_block_size)
num_blocks_y = math.ceil(ds.RasterYSize / y_block_size)
return num_blocks_x * num_blocks_y
def read_with_matching_block():
x_size, y_size = file_block_size
y_size = max(1, (mismatched_size ** 2) // (x_size * y_size) )
# print(x_size * y_size)
data = band.ReadAsArray(xoff, yoff, x_size, y_size)
def read_with_mismatched_block():
x_size, y_size = [mismatched_size, mismatched_size]
# print(x_size * y_size)
data = band.ReadAsArray(xoff, yoff, x_size, y_size)
match_block_time = timeit.timeit(read_with_matching_block, number=iterations)
mismatched_block_time = timeit.timeit(read_with_mismatched_block, number=iterations)
print('match_block_time', match_block_time)
print('mismatched_block_time', mismatched_block_time)
if match_block_time < mismatched_block_time:
print(f"Reading with matching block size {file_block_size} was {(mismatched_block_time - match_block_time) / match_block_time * 100:.2f}% faster")
else:
print(f"Reading with mismatched block size {mismatched_size} was {(match_block_time - mismatched_block_time) / mismatched_block_time * 100:.2f}% faster")
print(f'The difference in reality is small. Re-tiling would not be worth it to save {num_blocks(mismatched_size, mismatched_size) * mismatched_block_time/iterations} seconds')
print(f'Reading with matching blocks would take {num_blocks(*file_block_size) * match_block_time/iterations} seconds')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment