Skip to content

Commit adfdb05

Browse files
perf: lower cpu cycles in track_progress
- Pre compute all possible progress bars at once then reuse via a tuple - Remove math.floor reliance - use format specifier to round instead of round()
1 parent b7a66d8 commit adfdb05

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

megadl/helpers/pyros.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
# Description: Tools and helper functions related to pyrogram
55

66
from time import time
7-
from math import floor
87
from humans import human_time, human_bytes
98

109

10+
# pre compute possible progress bars
11+
PROGRESS_BARS = tuple(
12+
f"[{'█'*i}{'░'*(20-i)}]"
13+
for i in range(21)
14+
)
15+
1116
# Porogress bar for pyrogram
1217
# Improved version of SpEcHiDe's AnyDL-Bot
1318
async def track_progress(
@@ -25,8 +30,8 @@ async def track_progress(
2530
elapsed_time = human_time(elapsed_time)
2631
estimated_total_time = human_time(estimated_total_time)
2732

28-
filled = floor(percentage / 5)
29-
progress = f"[{'█'*filled}{'░'*(20-filled)}]\n**Process**: {round(percentage,2)}%\n"
33+
filled = min(int(percentage) // 5, 20)
34+
progress = f"{PROGRESS_BARS[filled]}\n**Process**: {percentage:.2f}%\n"
3035

3136
pmsg = f"{progress}{human_bytes(current)} of {human_bytes(total)}\n**Speed:** {human_bytes(speed)}/s\n**ETA:** {estimated_total_time if estimated_total_time != '' else '0 s'}\n\n\n**Powered by @NexaBotsUpdates**"
3237
try:

0 commit comments

Comments
 (0)