Advertisement
dan-masek

Untitled

May 1st, 2017
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. from timeit import default_timer as timer
  2.  
  3. import cv2
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6.  
  7.  
  8. def time_fn(fn, img, iters=16):
  9.     start = timer()
  10.     for i in range(iters):
  11.         fn(img)
  12.     end = timer()
  13.     return ((end - start) / iters) * 1000
  14.    
  15.  
  16. # By http://stackoverflow.com/users/7938192/liam-lawrence
  17. def foo1(img):
  18.     pxList = ''
  19.     # The height and width of your Mat
  20.     height = np.size(img, 0)
  21.     width = np.size(img, 1)
  22.  
  23.     # Iterates through the values of your Mat and stores them in pxList
  24.     for i in range(height):
  25.         for j in range(width):
  26.             pxList = pxList + " " + str(img[i][j])
  27.  
  28.     pxList = pxList[1:] # Drop the first space
  29.     return pxList
  30.  
  31. def foo2(img):
  32.     return ' '.join(map(str,img.flatten().tolist()))
  33.    
  34. def foo3(img):
  35.     return ' '.join(map(str,img.flatten()))
  36.    
  37. # Based on idea by http://stackoverflow.com/users/7847456/manel-fornos
  38. def foo4(img):
  39.     return str(img.flatten().tolist()).strip('[]').replace(',','')
  40.    
  41.    
  42.    
  43. def run_test(sz, iters):
  44.     test_img = np.ones((sz, sz),np.uint8) * 255
  45.  
  46.  
  47.     a = time_fn(foo1, test_img, iters)
  48.     b = time_fn(foo2, test_img, iters)
  49.     c = time_fn(foo3, test_img, iters)
  50.     d = time_fn(foo4, test_img, iters)
  51.  
  52.     return (sz*sz,a,b,c,d)
  53.  
  54. results = [
  55.     foo1(img = np.ones((128, 128),np.uint8) * 255)
  56.     , foo2(img = np.ones((128, 128),np.uint8) * 255)
  57.     , foo3(img = np.ones((128, 128),np.uint8) * 255)
  58.     , foo4(img = np.ones((128, 128),np.uint8) * 255)
  59. ]
  60.  
  61. print results[0] == results[1]
  62. print results[0] == results[2]
  63. print results[0] == results[3]
  64.  
  65. times = []
  66. for i in range(1,17):
  67.     times.append(run_test(32*i, 4))
  68.     print times[-1]
  69.  
  70. times = np.array(times)
  71.  
  72.  
  73. def plot_times(times):
  74.     fig = plt.figure(figsize=(12, 8), dpi=200)
  75.  
  76.     plt.plot(times[:,0], times[:,1], 'b+-', label='Variant 1')
  77.     plt.plot(times[:,0], times[:,2], 'y+-', label='Variant 2')
  78.     plt.plot(times[:,0], times[:,3], 'r+-', label='Variant 3')
  79.     plt.plot(times[:,0], times[:,4], 'g+-', label='Variant 4')
  80.  
  81.     plt.title('Algorithm timings')
  82.     plt.xlabel('Pixel count')
  83.     plt.ylabel('Iteration time (ms)')
  84.     plt.legend()
  85.  
  86.     fig.tight_layout()
  87.     fig.savefig('ibad_timing_1234.png', dpi=fig.dpi)
  88.     plt.close(fig)
  89.    
  90. def plot_times2(times):
  91.     fig = plt.figure(figsize=(12, 8), dpi=200)
  92.  
  93.     #plt.plot(times[:,0], times[:,1], 'b+-', label='Variant 1')
  94.     plt.plot(times[:,0], times[:,2], 'y+-', label='Variant 2')
  95.     #plt.plot(times[:,0], times[:,3], 'r+-', label='Variant 3')
  96.     plt.plot(times[:,0], times[:,4], 'g+-', label='Variant 4')
  97.  
  98.     plt.title('Algorithm timings')
  99.     plt.xlabel('Pixel count')
  100.     plt.ylabel('Iteration time (ms)')
  101.     plt.legend()
  102.  
  103.     fig.tight_layout()
  104.     fig.savefig('ibad_timing_24.png', dpi=fig.dpi)
  105.     plt.close(fig)
  106.  
  107.  
  108. plot_times(times)
  109. plot_times2(times)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement