python - 计算完成前剩余的时间

标签 python time

我想知道如何计算示例所需的时间:

Complete a brute force word list.

我知道如何使用时间功能并及时测量, 但问题是我需要找出程序本身需要多长时间...

这是我昨天做的代码

import itertools, math
import os
Alphabet = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890") # Add or remove whatevs you think will be in the password you're cracking (example, [symbols])
counter = 1
CharLength = 1
range_num = int(raw_input("Enter range: "))
stopper = range_num + 1
filename = "bruteforce_%r.txt" % (range_num)
f = open(filename, 'a')
#n_1 = len(Alphabet)
#n_2 = n_1 - 1 # <-- total useless peice of garbage that could of been great in vurtual life
#n_3 = '0' * n_2
#n = '1' + n_3
x = range_num
y = len(Alphabet)
amount = math.pow(y, x)
total_items = math.pow(y, x)
for CharLength in range(range_num, stopper):
    passwords = (itertools.product(Alphabet, repeat = CharLength))

    for i in passwords:
        counter += 1
        percentage = (counter / total_items) * 100
        amount -= 1
        i = str(i)
        i = i.replace("[", "")
        i = i.replace("]", "")
        i = i.replace("'", "")
        i = i.replace(" ", "")
        i = i.replace(",", "")
        i = i.replace("(", "")
        i = i.replace(")", "")
        f.write(i)
        f.write('\n')
        print "Password: %r\tPercentage: %r/100\tAmount left: %r" % (i, int(percentage), amount)
        if i == '0'* range_num:
            print "*Done"
            f.close()
            exit(0)
        else:
            pass

这是我成功实现的定时器功能

#import winsound # Comment this out if your using linux
import os 
import time
from sys import exit

print "This is the timer\nHit CTRL-C to stop the timer\nOtherwise just let         it rip untill the time's up"
hours = int(raw_input('Enter the hours.\n>>> '))
os.system('clear') # Linux
#os.system('cls')   # Windows

minutes = int(raw_input('Enter the minutes.\n>>> '))
os.system('clear') # linux
#os.system('cls')   # Windows

seconds = int(raw_input('Enter the seconds.\n>>> '))
os.system('clear') # Linux
#os.system('cls')   # Windows

stop_time = '%r:%r:%r' % (hours, minutes, seconds)

t_hours = 00
t_minutes = 00
t_seconds = 00

while t_seconds <= 60:
    try:
        os.system('clear') # Linux
        #os.system('cls') # Windows
        current_time = '%r:%r:%r' % (t_hours, t_minutes, t_seconds)
        print current_time
        time.sleep(1)
        t_seconds+=1

        if current_time == stop_time:
            print "// Done"
            #winsound.Beep(500,1000)
            #winsound.Beep(400,1000)
            break

        elif t_seconds == 60:
            t_minutes+=1
            t_seconds=0

        elif t_minutes == 60:
            t_hours+=1
            t_minutes = 00

    except KeyboardInterrupt:
        print "Stopped at: %r:%r:%r" % (t_hours, t_minutes, t_seconds)
        raw_input("Hit enter to continue\nHit CTRL-C to end")
        try:
            pass

        except KeyboardInterrupt:
            exit(0)

现在我只是想不出如何再做一次,而是计算需要多长时间而不是需要多长时间...

最佳答案

这是我的实现,它以 H:M:S 格式返回耗时、剩余时间和完成时间。

def calcProcessTime(starttime, cur_iter, max_iter):

    telapsed = time.time() - starttime
    testimated = (telapsed/cur_iter)*(max_iter)

    finishtime = starttime + testimated
    finishtime = dt.datetime.fromtimestamp(finishtime).strftime("%H:%M:%S")  # in time

    lefttime = testimated-telapsed  # in seconds

    return (int(telapsed), int(lefttime), finishtime)

例子:

import time
import datetime as dt

start = time.time()
cur_iter = 0
max_iter = 10

for i in range(max_iter):
    time.sleep(5)
    cur_iter += 1
    prstime = calcProcessTime(start,cur_iter ,max_iter)
    print("time elapsed: %s(s), time left: %s(s), estimated finish time: %s"%prstime)

输出:

time elapsed: 5(s), time left: 45(s), estimated finish time: 14:28:18
time elapsed: 10(s), time left: 40(s), estimated finish time: 14:28:18
time elapsed: 15(s), time left: 35(s), estimated finish time: 14:28:18
....

关于python - 计算完成前剩余的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44926127/

相关文章:

java - 使用Java LocalTime.parse方法

javascript - 如何在 Javascript 中让 AM/PM 与时间一起正确工作

python - 根据 Pandas 中另一列的索引从一列获取数据

java - 以这种格式显示 Java 8 Time api "HH:mma"(05 :53PM) and add set Time with only minutes

python - 这个 Max Counters codility 挑战的解决方案有什么问题

python - gsutil ssl.SSLError : [Errno 185090050]

android - 如何在 Android 中获取当前时间和日期

java - 识别正在播放的音频文件的运行时间

Apple Store Connect 的 Python JSON Web token (JWT)GET 请求 401 错误

Python 删除空格