[英]How can I check the number of iterations per second without using time module?
I would like to check how many iterations are completed per second or how many urllib requests are sent per second, but without slowing down the program at all (which time.sleep() seems to do). 我想检查每秒完成多少次迭代或每秒发送多少个urllib请求,但根本不减慢程序的速度(哪个时间。sleep()似乎可以做到)。 This is the program, for example: 这是程序,例如:
while True:
url = urllib.request.urlopen("google.com")
I would also like for the requests/per second counter to update in miliseconds, so it may print something like 我还希望请求/每秒计数器在毫秒内更新,因此它可能会打印类似
"2.83 requests per second" and then update to "2.93 requests per second" if the loop iterates faster. 如果循环迭代得更快,则“每秒2.83个请求”,然后更新为“每秒2.93个请求”。
I'd use time
and a counter to accomplish this. 我会花time
和一个计数器来完成这项工作。 I also added a custom generator (because I couldn't find one within Python's libraries) that allows you to both set and access the i
value. 我还添加了一个自定义生成器(因为在Python的库中找不到该生成器),该生成器允许您设置和访问i
值。
import time, itertools
def byRefCount():
i = [0]
while True:
yield i[0],i
i[0] += 1
last = time.time()
for i,ref in byRefCount():
url = urllib.request.urlopen("google.com")
if time() - last > 1:
print(i / (time.time()-last))
ref[0] = 0
last = time.time()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.