Coverage for gpaw/lrtddft2/eta.py: 72%
43 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-14 00:18 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-14 00:18 +0000
1import time
4class QuadraticETA:
5 def __init__(self, min_times=10):
6 self.times = []
7 self.min_times = max(min_times, 3)
9 def update(self):
10 self.times.append(time.time())
12 def eta(self, n):
13 if (len(self.times) < self.min_times):
14 # print '%6d' % (len(self.times))
15 return 0.0
17 ns = int(len(self.times) / 3.)
19 y1 = 0.0
20 for i in range(len(self.times) - 3 * ns, len(self.times) - 2 * ns):
21 y1 += self.times[i]
22 y1 /= ns
23 x1 = (len(self.times) - 3 * ns + len(self.times) - 2 * ns) / 2.
25 y2 = 0.0
26 for i in range(len(self.times) - 2 * ns, len(self.times) - ns):
27 y2 += self.times[i]
28 y2 /= ns
29 x2 = (len(self.times) - 2 * ns + len(self.times) - 1 * ns) / 2.
31 y3 = 0.0
32 for i in range(len(self.times) - ns, len(self.times)):
33 y3 += self.times[i]
34 y3 /= ns
35 x3 = (len(self.times) - 1 * ns + len(self.times) - 0 * ns) / 2.
37 # 2nd order fit:
38 # y(x) = a * x**2 + b * x + c
39 # y'(x) = 2a * x + b
40 # y''(x) = 2a
41 # y2''(x2) = 2 a = [(y3-y2)/(x3-x2) -
42 # (y2-y1)/(x2-x1)] / [.5*(x3+x2)-.5*(x2+x1)]
43 # y2'(x2) = 2*a*x + b = (y3-y1)/(x3-x1)
44 # y2(x2) = a * x2**2 + b * x2 + c
45 a = .5 * ((y3 - y2) / (x3 - x2) - (y2 - y1) /
46 (x2 - x1)) / (.5 * (x3 + x2) - .5 * (x2 + x1))
47 b = (y3 - y1) / (x3 - x1) - 2 * a * x2
48 c = y2 - a * x2 * x2 - b * x2
50 # print x1,y1
51 # print x2,y2
52 # print x3,y3
54 # print a
55 # print b
56 # print c
58 return a * n * n + b * n + c - time.time()
61if __name__ == "__main__":
62 eta = QuadraticETA(10)
64 N = 100
66 print(time.time())
67 t0 = time.time()
68 for i in range(N):
69 eta.update()
70 eta.eta(N + 1)
71 for j in range(i + 1):
72 time.sleep(.001)
74 t1 = time.time()
76 print(time.time())
78 print(t1 - t0)