Skip to content

Instantly share code, notes, and snippets.

@CTimmerman
Last active February 6, 2022 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CTimmerman/241a09b4f8f62624a90a592f964d5e83 to your computer and use it in GitHub Desktop.
Save CTimmerman/241a09b4f8f62624a90a592f964d5e83 to your computer and use it in GitHub Desktop.
Progress bar comparison test
"""Progress bar remaining time estimation comparison test.
See:
https://stackoverflow.com/questions/933242/smart-progress-bar-eta-computation/18524110#18524110
2017-07-14 v1.0 by Cees Timmerman
2022-02-05 v1.1 Migrate to Python 3 and lint some.
"""
import random
from typing import List
def function_a(done, time_passed):
"""Estimate based on total time passed."""
try:
remaining = time_passed / done - time_passed
return remaining
except ZeroDivisionError:
return -1
def average(last=0):
"""Return average of last outputs of decorated function."""
window: List[float] = []
def inner(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if last > 1:
window[:] = window[-(last-1):] + [result]
elif last == 1:
window[:] = [result]
else:
window.append(result)
return sum(window) / len(window)
return wrapper
return inner
@average(last=3)
def function_b(done, step_time):
"""Estimate based on latest progress time. Erratic. A 40% better."""
try:
return 100 * (1-done) * step_time
except ZeroDivisionError:
return -1
def main():
"""Run the test."""
time_passed = 0.
done = 0.
a_samples: List[float] = []
b_samples: List[float] = []
for i in range(101):
step_time = random.random() * 5 # nosec
time_passed += step_time
done = i / 100.
try:
a_remaining = function_a(done, time_passed)
a_samples.append(a_remaining)
b_remaining = function_b(done, step_time)
b_samples.append(b_remaining)
if i % 1 == 1:
print('%3.0f%% %6.2fs A: %6.2fs B: %6.2fs'
% (done*100, time_passed, a_remaining, b_remaining))
except ZeroDivisionError as ex:
print(time_passed, done, ex)
total_time = time_passed
a_err_total = 0
b_err_total = 0
for i in range(101):
remaining = total_time - i * total_time / 100
a_remaining = a_samples[i]
b_remaining = b_samples[i]
a_err = a_remaining - remaining
b_err = b_remaining - remaining
a_err_total += abs(a_err)
b_err_total += abs(b_err)
print(
f"{i:3.0f}% {remaining:6.2f}s A: {a_remaining:6.2f}. "
f"{a_remaining - remaining:7.2f} off, {a_err_total:7.2f} total."
f" B: {b_remaining:6.2f}. {b_remaining - remaining:7.2f} off, "
f"{b_err_total:7.2f} total. A is "
f"{(1 - a_err_total / b_err_total) * 100:7.2f}% better than B.")
if __name__ == "__main__":
main()
0% 219.67s A: -1.00. -220.67 off, 220.67 total. B: 71.63. -148.04 off, 148.04 total. A is -49.06% better than B.
1% 217.47s A: 202.37. -15.10 off, 235.76 total. B: 101.54. -115.93 off, 263.97 total. A is 10.68% better than B.
2% 215.27s A: 289.99. 74.72 off, 310.48 total. B: 194.25. -21.02 off, 284.99 total. A is -8.95% better than B.
3% 213.08s A: 270.91. 57.83 off, 368.32 total. B: 249.93. 36.85 off, 321.84 total. A is -14.44% better than B.
4% 210.88s A: 274.62. 63.74 off, 432.05 total. B: 304.14. 93.27 off, 415.11 total. A is -4.08% better than B.
5% 208.68s A: 245.53. 36.84 off, 468.90 total. B: 224.46. 15.78 off, 430.89 total. A is -8.82% better than B.
6% 206.49s A: 217.33. 10.85 off, 479.75 total. B: 174.67. -31.81 off, 462.70 total. A is -3.68% better than B.
7% 204.29s A: 202.86. -1.43 off, 481.18 total. B: 119.92. -84.37 off, 547.06 total. A is 12.04% better than B.
8% 202.09s A: 183.10. -18.99 off, 500.17 total. B: 93.08. -109.01 off, 656.07 total. A is 23.76% better than B.
9% 199.90s A: 176.16. -23.74 off, 523.91 total. B: 108.83. -91.07 off, 747.14 total. A is 29.88% better than B.
10% 197.70s A: 161.03. -36.66 off, 560.57 total. B: 79.65. -118.05 off, 865.19 total. A is 35.21% better than B.
11% 195.50s A: 176.83. -18.67 off, 579.24 total. B: 177.19. -18.31 off, 883.50 total. A is 34.44% better than B.
12% 193.31s A: 194.76. 1.46 off, 580.70 total. B: 269.63. 76.32 off, 959.82 total. A is 39.50% better than B.
13% 191.11s A: 191.63. 0.52 off, 581.22 total. B: 315.72. 124.61 off, 1084.43 total. A is 46.40% better than B.
14% 188.91s A: 181.64. -7.28 off, 588.49 total. B: 224.94. 36.03 off, 1120.46 total. A is 47.48% better than B.
15% 186.72s A: 174.61. -12.10 off, 600.59 total. B: 122.28. -64.44 off, 1184.90 total. A is 49.31% better than B.
16% 184.52s A: 173.97. -10.55 off, 611.15 total. B: 127.09. -57.43 off, 1242.32 total. A is 50.81% better than B.
17% 182.32s A: 164.69. -17.63 off, 628.78 total. B: 116.77. -65.55 off, 1307.88 total. A is 51.92% better than B.
18% 180.13s A: 172.28. -7.84 off, 636.62 total. B: 193.19. 13.07 off, 1320.95 total. A is 51.81% better than B.
19% 177.93s A: 161.66. -16.27 off, 652.89 total. B: 130.93. -47.00 off, 1367.94 total. A is 52.27% better than B.
20% 175.73s A: 160.68. -15.05 off, 667.94 total. B: 174.48. -1.25 off, 1369.20 total. A is 51.22% better than B.
21% 173.54s A: 155.63. -17.91 off, 685.85 total. B: 94.34. -79.20 off, 1448.40 total. A is 52.65% better than B.
22% 171.34s A: 151.53. -19.81 off, 705.66 total. B: 127.16. -44.18 off, 1492.58 total. A is 52.72% better than B.
23% 169.14s A: 152.23. -16.92 off, 722.58 total. B: 137.27. -31.87 off, 1524.45 total. A is 52.60% better than B.
24% 166.95s A: 149.09. -17.85 off, 740.43 total. B: 146.54. -20.40 off, 1544.85 total. A is 52.07% better than B.
25% 164.75s A: 153.16. -11.59 off, 752.01 total. B: 210.27. 45.52 off, 1590.37 total. A is 52.71% better than B.
26% 162.55s A: 155.26. -7.29 off, 759.30 total. B: 226.42. 63.87 off, 1654.24 total. A is 54.10% better than B.
27% 160.36s A: 158.73. -1.62 off, 760.93 total. B: 286.74. 126.38 off, 1780.62 total. A is 57.27% better than B.
28% 158.16s A: 160.38. 2.22 off, 763.14 total. B: 275.25. 117.09 off, 1897.71 total. A is 59.79% better than B.
29% 155.96s A: 160.42. 4.46 off, 767.60 total. B: 263.69. 107.72 off, 2005.43 total. A is 61.72% better than B.
30% 153.77s A: 162.58. 8.81 off, 776.42 total. B: 259.38. 105.62 off, 2111.05 total. A is 63.22% better than B.
31% 151.57s A: 165.47. 13.90 off, 790.32 total. B: 278.91. 127.34 off, 2238.39 total. A is 64.69% better than B.
32% 149.37s A: 165.66. 16.29 off, 806.60 total. B: 286.10. 136.73 off, 2375.12 total. A is 66.04% better than B.
33% 147.18s A: 164.07. 16.90 off, 823.50 total. B: 253.03. 105.86 off, 2480.97 total. A is 66.81% better than B.
34% 144.98s A: 161.19. 16.21 off, 839.71 total. B: 194.58. 49.60 off, 2530.58 total. A is 66.82% better than B.
35% 142.78s A: 159.81. 17.03 off, 856.74 total. B: 178.06. 35.28 off, 2565.86 total. A is 66.61% better than B.
36% 140.59s A: 159.20. 18.61 off, 875.35 total. B: 188.89. 48.30 off, 2614.16 total. A is 66.51% better than B.
37% 138.39s A: 152.89. 14.50 off, 889.85 total. B: 145.05. 6.66 off, 2620.82 total. A is 66.05% better than B.
38% 136.19s A: 148.29. 12.10 off, 901.95 total. B: 102.31. -33.89 off, 2654.71 total. A is 66.02% better than B.
39% 134.00s A: 145.62. 11.62 off, 913.57 total. B: 72.72. -61.28 off, 2715.98 total. A is 66.36% better than B.
40% 131.80s A: 139.76. 7.96 off, 921.53 total. B: 69.10. -62.70 off, 2778.68 total. A is 66.84% better than B.
41% 129.60s A: 135.83. 6.23 off, 927.76 total. B: 70.46. -59.14 off, 2837.83 total. A is 67.31% better than B.
42% 127.41s A: 136.68. 9.27 off, 937.03 total. B: 113.99. -13.41 off, 2851.24 total. A is 67.14% better than B.
43% 125.21s A: 134.09. 8.88 off, 945.91 total. B: 153.97. 28.76 off, 2880.00 total. A is 67.16% better than B.
44% 123.01s A: 130.80. 7.79 off, 953.69 total. B: 160.16. 37.14 off, 2917.14 total. A is 67.31% better than B.
45% 120.82s A: 130.52. 9.71 off, 963.40 total. B: 145.30. 24.49 off, 2941.63 total. A is 67.25% better than B.
46% 118.62s A: 130.32. 11.70 off, 975.10 total. B: 179.91. 61.29 off, 3002.92 total. A is 67.53% better than B.
47% 116.42s A: 128.43. 12.01 off, 987.11 total. B: 200.58. 84.16 off, 3087.08 total. A is 68.02% better than B.
48% 114.23s A: 123.55. 9.32 off, 996.43 total. B: 129.55. 15.33 off, 3102.41 total. A is 67.88% better than B.
49% 112.03s A: 120.06. 8.03 off, 1004.47 total. B: 75.78. -36.25 off, 3138.66 total. A is 68.00% better than B.
50% 109.83s A: 117.87. 8.03 off, 1012.50 total. B: 66.78. -43.05 off, 3181.71 total. A is 68.18% better than B.
51% 107.64s A: 113.70. 6.07 off, 1018.57 total. B: 71.90. -35.73 off, 3217.44 total. A is 68.34% better than B.
52% 105.44s A: 110.87. 5.43 off, 1024.00 total. B: 77.96. -27.48 off, 3244.93 total. A is 68.44% better than B.
53% 103.24s A: 109.16. 5.92 off, 1029.92 total. B: 82.86. -20.38 off, 3265.30 total. A is 68.46% better than B.
54% 101.05s A: 106.36. 5.32 off, 1035.24 total. B: 102.07. 1.02 off, 3266.33 total. A is 68.31% better than B.
55% 98.85s A: 102.95. 4.10 off, 1039.33 total. B: 88.23. -10.62 off, 3276.94 total. A is 68.28% better than B.
56% 96.65s A: 102.18. 5.53 off, 1044.86 total. B: 103.39. 6.73 off, 3283.68 total. A is 68.18% better than B.
57% 94.46s A: 100.63. 6.17 off, 1051.03 total. B: 124.34. 29.88 off, 3313.56 total. A is 68.28% better than B.
58% 92.26s A: 97.61. 5.35 off, 1056.38 total. B: 129.58. 37.32 off, 3350.88 total. A is 68.47% better than B.
59% 90.06s A: 95.66. 5.59 off, 1061.97 total. B: 106.64. 16.57 off, 3367.45 total. A is 68.46% better than B.
60% 87.87s A: 93.95. 6.08 off, 1068.06 total. B: 102.31. 14.44 off, 3381.89 total. A is 68.42% better than B.
61% 85.67s A: 90.28. 4.61 off, 1072.67 total. B: 86.35. 0.68 off, 3382.56 total. A is 68.29% better than B.
62% 83.47s A: 86.88. 3.40 off, 1076.07 total. B: 54.13. -29.35 off, 3411.91 total. A is 68.46% better than B.
63% 81.28s A: 83.38. 2.10 off, 1078.17 total. B: 13.30. -67.98 off, 3479.89 total. A is 69.02% better than B.
64% 79.08s A: 81.89. 2.81 off, 1080.99 total. B: 52.99. -26.09 off, 3505.98 total. A is 69.17% better than B.
65% 76.88s A: 78.52. 1.64 off, 1082.63 total. B: 49.05. -27.83 off, 3533.81 total. A is 69.36% better than B.
66% 74.69s A: 76.82. 2.13 off, 1084.76 total. B: 83.54. 8.86 off, 3542.67 total. A is 69.38% better than B.
67% 72.49s A: 73.56. 1.07 off, 1085.84 total. B: 42.66. -29.83 off, 3572.49 total. A is 69.61% better than B.
68% 70.29s A: 70.32. 0.03 off, 1085.86 total. B: 40.65. -29.65 off, 3602.14 total. A is 69.86% better than B.
69% 68.10s A: 67.69. -0.41 off, 1086.27 total. B: 16.06. -52.04 off, 3654.17 total. A is 70.27% better than B.
70% 65.90s A: 65.18. -0.72 off, 1086.99 total. B: 27.76. -38.14 off, 3692.31 total. A is 70.56% better than B.
71% 63.70s A: 62.31. -1.39 off, 1088.39 total. B: 31.43. -32.27 off, 3724.58 total. A is 70.78% better than B.
72% 61.51s A: 59.46. -2.05 off, 1090.44 total. B: 21.96. -39.54 off, 3764.12 total. A is 71.03% better than B.
73% 59.31s A: 57.95. -1.36 off, 1091.80 total. B: 41.76. -17.55 off, 3781.68 total. A is 71.13% better than B.
74% 57.11s A: 55.08. -2.03 off, 1093.83 total. B: 38.06. -19.05 off, 3800.73 total. A is 71.22% better than B.
75% 54.92s A: 53.08. -1.84 off, 1095.67 total. B: 55.40. 0.48 off, 3801.21 total. A is 71.18% better than B.
76% 52.72s A: 51.23. -1.49 off, 1097.16 total. B: 45.33. -7.39 off, 3808.60 total. A is 71.19% better than B.
77% 50.52s A: 48.50. -2.02 off, 1099.18 total. B: 45.57. -4.95 off, 3813.55 total. A is 71.18% better than B.
78% 48.33s A: 47.02. -1.31 off, 1100.49 total. B: 56.76. 8.43 off, 3821.98 total. A is 71.21% better than B.
79% 46.13s A: 44.78. -1.35 off, 1101.84 total. B: 45.06. -1.07 off, 3823.05 total. A is 71.18% better than B.
80% 43.93s A: 42.27. -1.66 off, 1103.50 total. B: 48.24. 4.31 off, 3827.36 total. A is 71.17% better than B.
81% 41.74s A: 40.30. -1.43 off, 1104.94 total. B: 33.84. -7.90 off, 3835.26 total. A is 71.19% better than B.
82% 39.54s A: 38.50. -1.04 off, 1105.98 total. B: 42.99. 3.45 off, 3838.71 total. A is 71.19% better than B.
83% 37.34s A: 36.12. -1.22 off, 1107.20 total. B: 44.24. 6.90 off, 3845.60 total. A is 71.21% better than B.
84% 35.15s A: 34.36. -0.78 off, 1107.98 total. B: 48.54. 13.39 off, 3858.99 total. A is 71.29% better than B.
85% 32.95s A: 32.32. -0.63 off, 1108.61 total. B: 40.75. 7.80 off, 3866.79 total. A is 71.33% better than B.
86% 30.75s A: 30.47. -0.28 off, 1108.89 total. B: 54.14. 23.38 off, 3890.17 total. A is 71.50% better than B.
87% 28.56s A: 28.18. -0.38 off, 1109.27 total. B: 38.49. 9.93 off, 3900.11 total. A is 71.56% better than B.
88% 26.36s A: 25.74. -0.62 off, 1109.90 total. B: 25.48. -0.88 off, 3900.99 total. A is 71.55% better than B.
89% 24.16s A: 23.34. -0.82 off, 1110.72 total. B: 7.08. -17.08 off, 3918.07 total. A is 71.65% better than B.
90% 21.97s A: 21.37. -0.59 off, 1111.31 total. B: 12.76. -9.20 off, 3927.27 total. A is 71.70% better than B.
91% 19.77s A: 19.20. -0.57 off, 1111.88 total. B: 17.47. -2.30 off, 3929.57 total. A is 71.70% better than B.
92% 17.57s A: 17.13. -0.44 off, 1112.33 total. B: 24.61. 7.04 off, 3936.61 total. A is 71.74% better than B.
93% 15.38s A: 15.08. -0.30 off, 1112.63 total. B: 20.77. 5.39 off, 3942.01 total. A is 71.78% better than B.
94% 13.18s A: 13.03. -0.15 off, 1112.78 total. B: 23.02. 9.84 off, 3951.85 total. A is 71.84% better than B.
95% 10.98s A: 10.96. -0.02 off, 1112.80 total. B: 22.30. 11.31 off, 3963.16 total. A is 71.92% better than B.
96% 8.79s A: 8.73. -0.05 off, 1112.85 total. B: 16.30. 7.51 off, 3970.68 total. A is 71.97% better than B.
97% 6.59s A: 6.57. -0.02 off, 1112.87 total. B: 11.45. 4.86 off, 3975.54 total. A is 72.01% better than B.
98% 4.39s A: 4.42. 0.03 off, 1112.90 total. B: 7.33. 2.94 off, 3978.48 total. A is 72.03% better than B.
99% 2.20s A: 2.19. -0.01 off, 1112.90 total. B: 5.61. 3.42 off, 3981.89 total. A is 72.05% better than B.
100% 0.00s A: 0.00. 0.00 off, 1112.90 total. B: 2.88. 2.88 off, 3984.78 total. A is 72.07% better than B.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment