-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeed_test.py
72 lines (47 loc) · 1.6 KB
/
speed_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import timeit
from PIL import Image
def to_image(src):
return Image.open(src)
def to_rgba_getdata(image):
sizes = image.size
width = sizes[0]
height = sizes[1]
pixels = list(image.getdata())
final = []
for y in range(height):
final.append(pixels[(y * width):((y + 1) * width)])
return final
def to_rgba_getpixel(image):
sizes = image.size
width = sizes[0]
height = sizes[1]
final = []
for y in range(height):
row = []
for x in range(width):
row.append(image.getpixel((x, y)))
final.append(row)
return final
def to_rgba_getdata_resize(image, shrink):
sizes = image.size
width = sizes[0]
height = sizes[1]
image = image.resize((int(width / shrink), int(height / shrink)))
pixels = list(image.getdata())
final = []
for y in range(height):
final.append(pixels[(y * width):((y + 1) * width)])
return final
n = 100
gd = timeit.timeit(lambda: to_rgba_getdata(to_image('resources/testing/test-image-2.png')), number=n)
print("=== getdata ===")
print(f"Total time: {gd} seconds")
print(f"Time per iter: {gd / n} seconds") # 0.1 secs
gdr = timeit.timeit(lambda: to_rgba_getdata_resize(to_image('resources/testing/test-image-2.png'), 8), number=n)
print("=== getdata_resize ===")
print(f"Total time: {gdr} seconds")
print(f"Time per iter: {gdr / n} seconds") # 0.01 secs
gp = timeit.timeit(lambda: to_rgba_getpixel(to_image('testing/test-image-2.png')), number=n)
print("=== getpixel ===")
print(f"Total time: {gp} seconds")
print(f"Time per iter: {gp / n} seconds") # 0.83 secs