简体   繁体   English

如何在不使用时间模块的情况下检查每秒的迭代次数?

[英]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.

相关问题 如何打印每秒迭代次数? - How to print iterations per second? 如何使用 aiohttp 确定每秒的请求数? - How do I determine the number of requests per second using aiohttp? 使用 % 运算符检查迭代次数 - Using % operator to check number of iterations 如何检查牛顿方法运行的迭代次数? - How can I check to see the number of iterations Newton's method takes to run? 如何在不影响结果确定性的情况下减少排序算法的迭代次数? - How can I reduce the number of iterations in a sorting algorithm without compromising the certainty of the results? 如何每次播放由多个动作激活的声音? - How can I play a sound activated by a number of actions per time? 如何在不使用 time.sleep() 的情况下进行 3 秒倒计时 function - How can I make a 3 second countdown without using the time.sleep() function 如何在不导入python模块的情况下检查python模块是否有效? - How can I check on runtime that a python module is valid without importing it? 如何扩展elasticsearch使其每秒能够索引大量文档? - How to scale elasticsearch that it can index a large number of documents per second? 如何在不使用 lambda 的情况下按第二个元素对列表进行排序? - How can I sort list by second element without using lambda?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM