-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcputest.py
More file actions
53 lines (38 loc) · 1.37 KB
/
cputest.py
File metadata and controls
53 lines (38 loc) · 1.37 KB
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
import random
import time
import math
import sys
import tracemalloc
def stress_test():
"""Stress test memory and CPU by generating and processing large data."""
print("Starting stress test...")
start_time = time.time()
tracemalloc.start()
data = [random.randint(1, 100) for _ in range(10_000_000)]
total = sum(x * x for x in data)
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
end_time = time.time()
print(f"Total sum of squares: {total}")
print(f"Memory used: {current / 1024**2:.2f} MB; Peak: {peak / 1024**2:.2f} MB")
print(f"Execution time: {end_time - start_time:.2f} seconds\n")
def floating_point_test():
"""Test floating-point precision and performance with heavy computation."""
print("Starting floating-point test...")
start_time = time.time()
errors = 0
results = []
for _ in range(1_000_000):
a = random.uniform(0.000001, 5.0)
b = random.uniform(0.000001, 5.0)
c = a * b / (a + b + 1e-10)
if math.isnan(c) or math.isinf(c):
errors += 1
results.append(c)
end_time = time.time()
print(f"Generated {len(results)} floating-point computations.")
print(f"Invalid results: {errors}")
print(f"Execution time: {end_time - start_time:.2f} seconds\n")
if __name__ == "__main__":
stress_test()
floating_point_test()