-
Notifications
You must be signed in to change notification settings - Fork 0
/
3q
84 lines (58 loc) · 1.86 KB
/
3q
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
import pygame
import time
from statistics import NormalDist
import matplotlib.pyplot as plt
import scipy.stats
from rust import Vec, Sphere, Line
pygame.init()
window = pygame.display.set_mode((800, 600))
run = True
window.fill(0)
times = []
def color(x, y):
start = time.perf_counter_ns()
screen_point = Vec(8.0 * x / 800, 6.0 * y / 600, 0.0)
screen_dir = Vec(0.0, 0.0, 1.0)
sphere = Vec(4.0, 3.0, 10.0)
radius = 2.0
light = Vec(8.0, 0.0, 7.0)
line = Line(screen_point, screen_dir)
sphere = Sphere(sphere, radius)
intersect = sphere.intersect(line)
if intersect is None:
return (100, 100, 100)
normal = sphere.get_normal(intersect)
light_ray = (light - intersect).normal()
result = (max(int(normal * light_ray * 255), 30), 0, 0)
end = time.perf_counter_ns()
times.append(end - start)
return result
start = 0
j = 0
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for i in range(start, min(800 * 600, start + 200)):
x = i % 800
y = i // 800
window.set_at((x, y), color(x, y))
start += 200
if j % 1000 == 0:
pygame.image.save(pygame.display.get_surface(), f"{j}.png")
j += 1
pygame.display.flip()
pygame.quit()
normal_dist = NormalDist.from_samples(times)
_, bins, _ = plt.hist(times, bins=100, density=True)
plt.plot(bins, scipy.stats.norm.pdf(bins, normal_dist.mean, normal_dist.stdev))
plt.title(
f"Performance (μ={normal_dist.mean:.2f}, σ={normal_dist.stdev:.2f}, N={len(times)})"
)
plt.xlabel("nanoseconds")
plt.ylabel("Probability")
plt.axvline(normal_dist.mean, color="r")
plt.axvline(normal_dist.mean - normal_dist.stdev, color="r", linestyle="--")
plt.axvline(normal_dist.mean + normal_dist.stdev, color="r", linestyle="--")
plt.xlim(0, 20000)
plt.savefig("performance.png")