python測試函式的使用時間

2022-05-17 19:27:56 字數 1625 閱讀 2894

1. 使用裝飾器來衡量函式執行時間

有乙個簡單方法,那就是定義乙個裝飾器來測量函式的執行時間,並輸出結果:(**通用3.x)

import

time

from functools import

wraps

deffn_timer(function):

@wraps(function)

def function_timer(*args, **kwargs):

t0 =time.time()

result = function(*args, **kwargs)

t1 =time.time()

print("

total time running %s: %s seconds

" %(function.

__name__, str(t1-t0))

)return

result

return function_timer

要測試函式的使用時間時,只需要@fn_timer裝飾器即可。

@fn_timer

defmyfunction(...):

...

下面是測試:

in [14]: @fn_timer

...:

defnorm(a):

...:

return sum(a**2)**(1/2)

...:

in [15]: @fn_timer

...:

defnorm2(a):

...:

return

la.norm(a)

...:

in [16]: norm(a)

total time running norm: 0.0seconds

out[16]: 4.7958315233127191in [17]: norm2(a)

total time running norm2: 0.0seconds

out[17]: 4.7958315233127191in [18]: a = np.random.randint(-3,3,(10000,))

in [19]: norm(a)

total time running norm: 0.0010035037994384766seconds

out[19]: 177.92695130305583in [20]: norm2(a)

total time running norm2: 0.001010894775390625seconds

out[20]: 177.92695130305583in [21]: a = np.random.randint(-3,3,(50000,))

in [22]: norm(a)

total time running norm: 0.005008220672607422seconds

out[22]: 397.39275282772837in [23]: norm2(a)

total time running norm2: 0.0seconds

out[23]: 397.39275282772837

Entity SQL使用時間函式

sql server datepart 函式 定義和用法 datepart 函式用於返回日期 時間的單獨部分,比如年 月 日 小時 分鐘等等。語法datepart datepart,date date 引數是合法的日期表示式。datepart 引數可以是下列的值 datepart 縮寫 年 yy,y...

使用時間服務

時間服務用於提供公信的時間資訊。一般的 時間服務無所謂,對於商務和軍事就很有用了。比如 商務 網際網路軟體許可過期,電子合同什麼時候算是違約,稅務部門什麼時候開始算你滯報金 軍事 記得以前電影總看見 3 00總攻開始,大家對時。不過現在都已經資訊化了,計算機不能掏出一塊omega,然後扭表針。方法 ...

管理時間,合理使用時間!

時間管理的敵人 吃雞 王者榮耀 朋友圈 抖音 微博 負能量 抱怨 煩躁 急功急利 委屈 苦勞的心態 佛系 安於現狀 時間管理的朋友 克服拖延 保持專注 重視計畫 安排優先順序 今日事今日畢 利用碎片時間 拖延症 自我調節失敗。在能夠預料後果有害的情況下,仍然把計畫要做的事情往後推遲的一種行動。克服拖...