53 import os.path
as path
61 dir = path.dirname(path.realpath(__file__))
63 parser = argparse.ArgumentParser(description=
'Benchmark for overlay_alpha')
64 parser.add_argument(
'--no-simd', dest=
'SIMD', action=
'store_false',
65 help=
'Disable SIMD intrinsics')
66 parser.add_argument(
'--N', dest=
'N', type=int, default=25,
67 help=
'The number of different sizes to test')
68 parser.add_argument(
'--min', dest=
'min_size', type=int, default=10,
69 help=
'The size in pixels of the smallest image in the test')
70 parser.add_argument(
'--max', dest=
'max_size', type=int, default=2000,
71 help=
'The size in pixels of the largest image in the test')
72 parser.add_argument(
'--it', dest=
'max_iterations', type=int, default=10,
73 help=
'The number of test iterations for the largest images')
74 parser.add_argument(
'--rescale', dest=
'rescale', choices=[
'div255_round',
75 'div255_round_approx',
'div255_floor',
'div256_round',
76 'div256_floor'], default=
'div255_round',
77 help=
'The number of test iterations for the largest images')
78 args = parser.parse_args()
82 uint8_t_p = ctypes.POINTER(ctypes.c_uint8)
83 size_t = ctypes.c_size_t
87 so += platform.machine()
88 if not args.SIMD: so +=
"-no-simd"
90 dll = ctypes.cdll.LoadLibrary(path.join(dir, so))
91 overlay_alpha = dll[
'overlay_alpha_stride_' + args.rescale]
92 overlay_alpha.argtypes = [
101 overlay_alpha.restype = void
104 sizes = np.linspace(args.min_size, args.max_size, args.N, dtype=np.int)
105 times = np.zeros((args.N, ))
108 for i, size
in enumerate(sizes):
109 print(i + 1,
'/', args.N,
':', size)
112 bg_img = np.random.randint(255, size=(size, size, 4), dtype=np.uint8)
113 fg_img = np.random.randint(255, size=(size, size, 4), dtype=np.uint8)
114 out_img = np.zeros((size, size, 4), dtype=np.uint8)
115 bg_img_p = bg_img.ctypes.data_as(uint8_t_p)
116 fg_img_p = fg_img.ctypes.data_as(uint8_t_p)
117 out_img_p = out_img.ctypes.data_as(uint8_t_p)
120 iterations =
int(round(args.max_size * args.max_iterations / size))
121 start_time = time.perf_counter()
122 for _
in range(iterations):
123 overlay_alpha(bg_img_p, fg_img_p, out_img_p, size, size, size, size)
124 end_time = time.perf_counter()
125 times[i] = (end_time - start_time) / iterations
128 results = np.column_stack((sizes, times))
129 simd = (
' simd ' if args.SIMD
else ' no-simd ')
130 name = str(time.asctime()) + simd + args.rescale +
' ' + platform.machine()
131 np.savetxt(name +
'.csv', results, delimiter=
',')
134 import matplotlib.pyplot
as plt
135 plt.plot(sizes, times,
'.-')
136 plt.xlabel(
'Image size [pixels]')
137 plt.ylabel(
'Time [s]')
138 plt.savefig(name +
'.svg')