首页 > 解决方案 > 如何创建一个不会显着减慢程序速度的百分比跟踪器

问题描述

我有一个关于百分比跟踪器的问题。我有一个生成工具的百分比跟踪器,百分比跟踪器显着减慢了它的速度。(5 秒对 35 秒)

def wiiu():
    time.sleep(0.2)
    track = 100000
    os.remove("Codes.txt")
    file = open("Codes.txt","w")
    clear()
    print("3ds and Wii-U Friend Code Generator")
    print("")
    print("How many codes to generate: ")
    amt = int(input())
    print("Generating...")
    total = 0
    total1 = 0
    file = open("codes.txt","w") 
    while total < amt:
        pc = 200/(int(amt)+(int(total)))
        total1 + 1
        pc1 = pc + 100
        fcpt1 = str(randint(1000, 9999)) + "-" + str(randint(1000, 9999)) + "-" + str(randint(1000,9999))
        total = total+1
        ctypes.windll.kernel32.SetConsoleTitleW("Friend Code Generator - " + str(round(total*pc)) + "% completed")
        if total == track:
            print(str(total) + " generated.")
            track = track + 100000
        file.write(str(fcpt1)+"\n")
    clear()
    file.close()
    if total == 1:
        print("Completed! Generated "+ str(total) +" code!")
    else:
        print("Completed! Generated "+ str(total) +" codes!")
    ctypes.windll.kernel32.SetConsoleTitleW("Friend Code Generator - 2.0.4")
    a = input("Press enter to close! Thanks for using!")
    close()

任何帮助深表感谢!

标签: python

解决方案


如果没有正确的分析,我只能猜测,但可能ctypes.windll.kernel32.SetConsoleTitleW需要一些时间,尤其是在每次迭代运行时。也许每次都这样做100th会更快?

while total < amt:
        pc = 200/(int(amt)+(int(total)))
        total1 + 1
        pc1 = pc + 100
        fcpt1 = str(randint(1000, 9999)) + "-" + str(randint(1000, 9999)) + "-" + str(randint(1000,9999))
        total = total+1

        # Use the modulo operator to let it run every 100th time
        if total % 100 == 0:
            ctypes.windll.kernel32.SetConsoleTitleW("Friend Code Generator - " + str(round(total*pc)) + "% completed")
        if total == track:
            print(str(total) + " generated.")
            track = track + 100000
        file.write(str(fcpt1)+"\n")

推荐阅读