Skip to content

Instantly share code, notes, and snippets.

@jcupitt
Created March 30, 2023 10:38
Show Gist options
  • Save jcupitt/d8958b1bdb65a25ca039177a63fd744d to your computer and use it in GitHub Desktop.
Save jcupitt/d8958b1bdb65a25ca039177a63fd744d to your computer and use it in GitHub Desktop.
time fetch and crop for image read
#!/usr/bin/python3
import random
import time
import sys
import pyvips
if len(sys.argv) != 4:
print("usage: ./fetch-vs-crop.py IMAGE SIZE N-TILES")
size = int(sys.argv[2])
n_tiles = int(sys.argv[3])
print(f"{n_tiles} tiles, each {size}x{size} pixels")
image = pyvips.Image.new_from_file(sys.argv[1])
print(f"random read")
start = time.time()
for _ in range(n_tiles):
x = random.randint(0, image.width - size)
y = random.randint(0, image.height - size)
tile = image.crop(x, y, size, size)
# necessary to force evaluation
tile = tile.copy_memory()
end = time.time()
print(f"crop: time per tile = {1000 * (end - start) / n_tiles:.2f}ms")
start = time.time()
region = pyvips.Region.new(image)
for _ in range(n_tiles):
x = random.randint(0, image.width - size)
y = random.randint(0, image.height - size)
p = region.fetch(x, y, size, size)
end = time.time()
print(f"fetch: time per tile = {1000 * (end - start) / n_tiles:.2f}ms")
print(f"row-major sequential read")
image = pyvips.Image.new_from_file(sys.argv[1])
start = time.time()
tile_number = 0
for y in range(0, image.height - size, size):
for x in range(0, image.width - size, size):
tile = image.crop(x, y, size, size)
# necessary to force evaluation
tile = tile.copy_memory()
tile_number += 1
if tile_number > n_tiles:
break
if tile_number > n_tiles:
break
end = time.time()
print(f"crop: time per tile = {1000 * (end - start) / n_tiles:.2f}ms")
start = time.time()
tile_number = 0
region = pyvips.Region.new(image)
for y in range(0, image.height - size, size):
for x in range(0, image.width - size, size):
x = random.randint(0, image.width - size)
y = random.randint(0, image.height - size)
p = region.fetch(x, y, size, size)
tile_number += 1
if tile_number > n_tiles:
break
if tile_number > n_tiles:
break
end = time.time()
print(f"fetch: time per tile = {1000 * (end - start) / n_tiles:.2f}ms")
print(f"column-major sequential read")
image = pyvips.Image.new_from_file(sys.argv[1])
start = time.time()
tile_number = 0
for x in range(0, image.width - size, size):
for y in range(0, image.height - size, size):
tile = image.crop(x, y, size, size)
# necessary to force evaluation
tile = tile.copy_memory()
tile_number += 1
if tile_number > n_tiles:
break
if tile_number > n_tiles:
break
end = time.time()
print(f"crop: time per tile = {1000 * (end - start) / n_tiles:.2f}ms")
start = time.time()
tile_number = 0
region = pyvips.Region.new(image)
for x in range(0, image.width - size, size):
for y in range(0, image.height - size, size):
x = random.randint(0, image.width - size)
y = random.randint(0, image.height - size)
p = region.fetch(x, y, size, size)
tile_number += 1
if tile_number > n_tiles:
break
if tile_number > n_tiles:
break
end = time.time()
print(f"fetch: time per tile = {1000 * (end - start) / n_tiles:.2f}ms")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment