-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
126 lines (98 loc) · 3.41 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import numpy as np
import matplotlib.pyplot as plt
from time import perf_counter
import sys
import pyfract
import rsfract
def display_mandelbrot(size: int):
py_start = perf_counter()
py_image = pyfract.generate_mandelbrot(size)
py_end = perf_counter()
py_time = py_end - py_start
print(f'Python Mandelbrot set generation time: {py_time}')
rs_start = perf_counter()
rs_image = rsfract.generate_mandelbrot(size)
rs_end = perf_counter()
rs_time = rs_end - rs_start
print(f'Rust Mandelbrot set generation time: {rs_time}')
fig, axs = plt.subplots(1, 2)
img1 = axs[0].imshow(py_image)
axs[0].set_title('python')
img2 = axs[1].imshow(rs_image)
axs[1].set_title('rust')
for ax in axs:
ax.axis('off')
fig.suptitle('mandelbrot set')
plt.show()
def display_random_noise(size: int):
py_start = perf_counter()
py_image = pyfract.generate_noise(size)
py_end = perf_counter()
py_time = py_end - py_start
print(f'Python random noise generation time: {py_time}')
np_start = perf_counter()
np_image = np.random.random((size, size, 3))
np_end = perf_counter()
np_time = np_end - np_start
print(f'Numpy random noise generation time: {np_time}')
rs_start = perf_counter()
rs_image = rsfract.generate_noise(size)
rs_end = perf_counter()
rs_time = rs_end - rs_start
print(f'Rust random noise generation time: {rs_time}')
rs_start = perf_counter()
rs_thr_image = rsfract.generate_noise_threaded(size)
rs_end = perf_counter()
rs_time = rs_end - rs_start
print(f'Rust random noise generation time (multithreaded): {rs_time}')
rs_start = perf_counter()
rs_thr_lck_image = rsfract.generate_noise_threaded_with_locks(size)
rs_end = perf_counter()
rs_time = rs_end - rs_start
print(f'Rust random noise generation time (multithreaded with locks): {rs_time}')
rs_start = perf_counter()
rs_thr_par_image = rsfract.generate_noise_parallel(size)
rs_end = perf_counter()
rs_time = rs_end - rs_start
print(f'Rust random noise generation time (parallel): {rs_time}')
fig, axs = plt.subplots(3, 2)
axs[0][0].imshow(py_image)
axs[0][0].set_title('python')
axs[0][1].imshow(np_image)
axs[0][1].set_title('numpy')
axs[1][0].imshow(rs_image)
axs[1][0].set_title('rust')
axs[1][1].imshow(rs_thr_image)
axs[1][1].set_title('rust (multithreaded)')
axs[2][0].imshow(rs_thr_lck_image)
axs[2][0].set_title('rust (multithreaded with locks)')
axs[2][1].imshow(rs_thr_par_image)
axs[2][1].set_title('rust (parallel)')
for row in axs:
for ax in row:
ax.axis('off')
fig.suptitle('random noise')
plt.show()
def main():
while True:
print('''
Do you want to:
1. Generate Mandelbrot set image
2. Generate random noise image
3. Quit
''')
choice = int(input('Choose an option: '))
if choice == 1:
size = int(input('Size of the image in pixels: '))
while size%4 != 0:
size = int(input('The size should be divisble by 4: '))
display_mandelbrot(size)
choice = 0
elif choice == 2:
size = int(input('Size of the image in pixels: '))
display_random_noise(size)
choice = 0
elif choice == 3:
sys.exit()
if __name__ == "__main__":
main()