python - 如何创建一个不会显着减慢程序速度的百分比跟踪器
问题描述
我有一个关于百分比跟踪器的问题。我有一个生成工具的百分比跟踪器,百分比跟踪器显着减慢了它的速度。(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()
任何帮助深表感谢!
解决方案
如果没有正确的分析,我只能猜测,但可能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")
推荐阅读
- python-3.x - Skimage RAG 合并灰度图像
- node.js - 卡在 Node.js 中的 oracle Ref 光标
- vue.js - 条目模式“'./src/components/*.vue'”不匹配任何文件/构建 Web 组件 VUEJS
- sql-server - 比较两个不同列的时间并创建显示时间差异的第三列
- c# - 无法在 Interop.QBFC13.IQBSessionManager 从注册表中读取密钥(HRESULT 异常:0x80040150 (REGDB_E_READREGDB))
- python - 从生成器对象保存图像 - Python
- r - 使用字符串 r 标记特定模式
- python - 在python中组合具有相似值的csv行
- spring-boot - 字段需要一个找不到类型的 bean。
- angular - 离子3在手机上安装应用程序后白屏长时间启动?我怎么能解决这个问题?