簡體   English   中英

如何找出在 Python 中執行 function 一分鍾多少次?

[英]How to find out how many times a minute a function is executed in Python?

我有一個 function 我想知道這個 function 每分鍾執行多少次

例如,每 1 分鍾寫入一次:

204 哈希/米

這是我的 function

def test_hash_abc(difficulty):
    x = 0
    while 1:
        y = hashlib.sha256("hello world{}".format(x).encode('utf-8')).hexdigest()
        if y[0:len(difficulty)] == str(difficulty) :
            print(y)
            x+= 1
        else : x+= 1


test_hash_abc('ffff')

不能說每分鍾,但您可以使用line_profiler模塊,該模塊具有每個命中表的時間

您的 function 包含while 1在您的情況下這是一個無限循環(因為您永遠不會跳出循環)。 我假設您想查看while 1的代碼每分鍾運行多少次。 在這種情況下,請嘗試以下操作:

import hashlib
import time

def test_hash_abc(difficulty):
    x = 0
    counter = 0
    while 1:
        start = time.time()
        while time.time() - start < 60:
            y = hashlib.sha256("hello world{}".format(x).encode('utf-8')).hexdigest()
            if y[0:len(difficulty)] == str(difficulty) :
                print(y)
                x+= 1
            else:
                x+= 1
            counter += 1
        print('{} hash/m'.format(counter))

test_hash_abc('ffff')

請注意,刪除print(y)將導致每分鍾的執行次數更高。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM