Created
March 7, 2024 03:55
-
-
Save SarvagyaVaish/0e6b589e70a72e771076dd126cce1def to your computer and use it in GitHub Desktop.
This file contains 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 cv2 | |
import time | |
import numpy as np | |
IMG_SIZE = (250, 250) | |
NUM_ITERS = 500 | |
def compare_tobytes_runtimes(img1, name1, img2, name2): | |
t0 = time.perf_counter() | |
for _ in range(NUM_ITERS): | |
img1.tobytes() | |
img1_t = time.perf_counter() - t0 | |
t0 = time.perf_counter() | |
for _ in range(NUM_ITERS): | |
img2.tobytes() | |
img2_t = time.perf_counter() - t0 | |
print(f"Ratio of {name1}:{name2} = {(img1_t / img2_t):0.5f}") | |
if __name__ == "__main__": | |
# Random image | |
img_rand = np.random.randint(0, 256, IMG_SIZE, dtype=np.uint8) | |
# Save it, turning it into a "real" image and making this script self sufficient. | |
cv2.imwrite("real_image.png", img_rand) | |
# Real image | |
img_real = cv2.imread("real_image.png") | |
img_real = img_real[:, :, 0] | |
compare_tobytes_runtimes(img_real, "Real", img_rand, "Random") | |
# | |
# Why the near 100x difference? | |
# | |
# 1. Are they the same sizes? | |
assert img_rand.shape == img_real.shape | |
print("\nShape is the same:", img_rand.shape) | |
# 2. Are they the same data types? | |
assert img_rand.dtype == img_real.dtype | |
print("\nDtype is the same:", img_rand.dtype) | |
# 3. The actual data is different. Do different int8s takes different times to serialize? | |
for x in range(img_rand.shape[0]): | |
for y in range(img_rand.shape[1]): | |
img_real[x, y] = img_rand[x, y] | |
assert img_real.tobytes() == img_rand.tobytes() | |
print("\nRerunning timing comparison after copying data from 'random image' to 'real image'...") | |
compare_tobytes_runtimes(img_real, "Real", img_rand, "Random") | |
# | |
# Still large discrepency in runtimes | |
# Reload the 'real image' | |
# | |
img_real = cv2.imread("real_image.png") | |
img_real = img_real[:, :, 0] | |
print("\nCheck out the flags on the numpy matrices") | |
print("Real image:") | |
print(img_real.flags) | |
print("Random image:") | |
print(img_rand.flags) | |
# It's a memory layout / memory access related issue. | |
# Lets call copy() on the 'real image' first. | |
print("\nRerunning timing comparison after calling copy() on the 'real image'...") | |
img_real = img_real.copy() | |
compare_tobytes_runtimes(img_real, "Real", img_rand, "Random") | |
print("\nNOW THE TIMES ARE ABOUT THE SAME :)") | |
print("\nThe flags also match!") | |
print("Real image:") | |
print(img_real.flags) | |
print("Random image:") | |
print(img_rand.flags) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment