-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
85 lines (63 loc) · 2.26 KB
/
Copy pathbenchmark.py
File metadata and controls
85 lines (63 loc) · 2.26 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
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
import timeit
from collections.abc import Callable
from src.secant import secant
from src.newtons import newtons
from src.fixedPoint import fixed_point
from src.bisection import bisect
# Define functions and their derivatives
def easy_func(x: float) -> float:
return x**2 - 4
def easy_func_prime(x: float) -> float:
return 2 * x
def hard_func(x: float) -> float:
return x**3 - 2 * x + 2
def hard_func_prime(x: float) -> float:
return 3 * x**2 - 2
def really_hard_func(x: float) -> float:
return x**5 - x**4 + x**3 - x**2 + x - 1
def really_hard_func_prime(x: float) -> float:
return 5 * x**4 - 4 * x**3 + 3 * x**2 - 2 * x + 1
# Benchmark settings
APPROX = 0.1
TOL = 10**-5
N = 100
A = -2
B = 2
def benchmark_method(
method, func: Callable[[float], float], func_prime: Callable[[float], float]
):
if method == secant:
return lambda: secant(func, APPROX, APPROX - 0.01, TOL, N)
elif method == newtons:
return lambda: newtons(func, func_prime, APPROX, TOL, N)
elif method == fixed_point:
return lambda: fixed_point(func, APPROX, TOL, N)
elif method == bisect:
return lambda: bisect(func, A, B, TOL, N)
else:
raise ValueError("Invalid method")
if __name__ == "__main__":
methods = {
"Secant Method": secant,
"Newton's Method": newtons,
# "Fixed-Point Method": fixed_point,
"Bisection Method": bisect,
}
functions = {
"Easy Function": (easy_func, easy_func_prime),
# "Hard Function": (hard_func, hard_func_prime),
"Really Hard Function": (really_hard_func, really_hard_func_prime),
}
results = []
for func_name, (func, func_prime) in functions.items():
for method_name, method in methods.items():
print(f"running {method_name} on {func_name} ... ", end="")
timer = timeit.Timer(benchmark_method(method, func, func_prime))
print("done")
time_taken = timer.timeit(number=100)
results.append((method_name, func_name, time_taken))
print()
print(f"{'Method':<20} {'Function':<25} {'Time (s) for 100 runs':<25}")
print("=" * 80)
for method_name, func_name, time_taken in results:
print(f"{method_name:<20} {func_name:<25} {time_taken:<25.8f}")