Last active
March 7, 2024 03:55
-
-
Save SarvagyaVaish/8f7703fed612151e6eb6c319a5ae1bde 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? | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: