Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from timeit import default_timer as timer
- import cv2
- import numpy as np
- import matplotlib.pyplot as plt
- def time_fn(fn, img, iters=16):
- start = timer()
- for i in range(iters):
- fn(img)
- end = timer()
- return ((end - start) / iters) * 1000
- # By http://stackoverflow.com/users/7938192/liam-lawrence
- def foo1(img):
- pxList = ''
- # The height and width of your Mat
- height = np.size(img, 0)
- width = np.size(img, 1)
- # Iterates through the values of your Mat and stores them in pxList
- for i in range(height):
- for j in range(width):
- pxList = pxList + " " + str(img[i][j])
- pxList = pxList[1:] # Drop the first space
- return pxList
- def foo2(img):
- return ' '.join(map(str,img.flatten().tolist()))
- def foo3(img):
- return ' '.join(map(str,img.flatten()))
- # Based on idea by http://stackoverflow.com/users/7847456/manel-fornos
- def foo4(img):
- return str(img.flatten().tolist()).strip('[]').replace(',','')
- def run_test(sz, iters):
- test_img = np.ones((sz, sz),np.uint8) * 255
- a = time_fn(foo1, test_img, iters)
- b = time_fn(foo2, test_img, iters)
- c = time_fn(foo3, test_img, iters)
- d = time_fn(foo4, test_img, iters)
- return (sz*sz,a,b,c,d)
- results = [
- foo1(img = np.ones((128, 128),np.uint8) * 255)
- , foo2(img = np.ones((128, 128),np.uint8) * 255)
- , foo3(img = np.ones((128, 128),np.uint8) * 255)
- , foo4(img = np.ones((128, 128),np.uint8) * 255)
- ]
- print results[0] == results[1]
- print results[0] == results[2]
- print results[0] == results[3]
- times = []
- for i in range(1,17):
- times.append(run_test(32*i, 4))
- print times[-1]
- times = np.array(times)
- def plot_times(times):
- fig = plt.figure(figsize=(12, 8), dpi=200)
- plt.plot(times[:,0], times[:,1], 'b+-', label='Variant 1')
- plt.plot(times[:,0], times[:,2], 'y+-', label='Variant 2')
- plt.plot(times[:,0], times[:,3], 'r+-', label='Variant 3')
- plt.plot(times[:,0], times[:,4], 'g+-', label='Variant 4')
- plt.title('Algorithm timings')
- plt.xlabel('Pixel count')
- plt.ylabel('Iteration time (ms)')
- plt.legend()
- fig.tight_layout()
- fig.savefig('ibad_timing_1234.png', dpi=fig.dpi)
- plt.close(fig)
- def plot_times2(times):
- fig = plt.figure(figsize=(12, 8), dpi=200)
- #plt.plot(times[:,0], times[:,1], 'b+-', label='Variant 1')
- plt.plot(times[:,0], times[:,2], 'y+-', label='Variant 2')
- #plt.plot(times[:,0], times[:,3], 'r+-', label='Variant 3')
- plt.plot(times[:,0], times[:,4], 'g+-', label='Variant 4')
- plt.title('Algorithm timings')
- plt.xlabel('Pixel count')
- plt.ylabel('Iteration time (ms)')
- plt.legend()
- fig.tight_layout()
- fig.savefig('ibad_timing_24.png', dpi=fig.dpi)
- plt.close(fig)
- plot_times(times)
- plot_times2(times)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement