Created
November 13, 2018 09:55
-
-
Save ylogx/7b5d7f0957a4aa3c84c010f3d7f27643 to your computer and use it in GitHub Desktop.
Performance Analysis of Bytes IO vs File IO
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
""" | |
Usage: | |
>>> ipython | |
>>> %load perf_bytes_io.py | |
>>> <ENTER> | |
""" | |
import logging | |
import tempfile | |
import cv2 | |
import requests | |
url = 'https://chaudhary.page.link/test-zomato-img' # A jpeg image | |
response_object = requests.get(url) | |
def log(s): | |
# print(s) | |
pass | |
def file_io(): | |
import logging | |
import os | |
import tempfile | |
import cv2 | |
import requests | |
def download_image(url): | |
#logging.info('Downloading image from url: %s', url[:100]) | |
#response_object = requests.get(url) | |
file_descriptor, filename = tempfile.mkstemp(prefix='image-', suffix='.jpg') | |
logging.info('Saving file: %s', filename) | |
with open(file_descriptor, mode='wb') as f: | |
f.write(response_object.content) | |
return filename | |
url = 'https://chaudhary.page.link/test-zomato-img' | |
image_path = download_image(url) | |
img = cv2.imread(image_path) | |
resized_img = cv2.resize(img, (299, 299)) | |
# preprocess(resized_image) | |
# prediction_score = model.predict(resized_img) | |
os.remove(image_path) | |
log(resized_img.shape) | |
def bytes_io(): | |
from io import BytesIO | |
import cv2 | |
import numpy as np | |
import requests | |
url = 'https://chaudhary.page.link/test-zomato-img' | |
#response_object = requests.get(url) | |
image_data = BytesIO(response_object.content) | |
file_bytes = np.asarray(bytearray(image_data.read()), dtype=np.uint8) | |
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR) | |
image_data.close() | |
resized_img = cv2.resize(img, (299, 299)) | |
# preprocess(resized_image) | |
# prediction_score = model.predict(resized_img) | |
log(resized_img.shape) | |
def direct_decode(): | |
import cv2 | |
import numpy as np | |
import requests | |
url = 'https://chaudhary.page.link/test-zomato-img' | |
#response_object = requests.get(url) | |
file_bytes = np.asarray(bytearray(response_object.content), dtype=np.uint8) | |
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR) | |
resized_img = cv2.resize(img, (299, 299)) | |
# preprocess(resized_image) | |
# prediction_score = model.predict(resized_img) | |
log(resized_img.shape) | |
%timeit file_io() | |
%timeit bytes_io() | |
%timeit direct_decode() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment