欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

python檢測周末函數(shù) python星期函數(shù)怎么用

7種檢測Python程序運行時間、CPU和內(nèi)存占用的方法

1. 使用裝飾器來衡量函數(shù)執(zhí)行時間

10年積累的網(wǎng)站制作、成都網(wǎng)站設(shè)計經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先建設(shè)網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有新會免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

有一個簡單方法,那就是定義一個裝飾器來測量函數(shù)的執(zhí)行時間,并輸出結(jié)果:

import time

from functoolsimport wraps

import random

def fn_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

def random_sort(n):

return sorted([random.random() for i in range(n)])

if __name__== "__main__":

random_sort(2000000)

輸出:Total time running random_sort: 0.6598007678985596 seconds

使用方式的話,就是在要監(jiān)控的函數(shù)定義上面加上 @fn_timer 就行了

或者

# 可監(jiān)控程序運行時間

import time

import random

def clock(func):

def wrapper(*args, **kwargs):

? ? start_time= time.time()

? ? result= func(*args, **kwargs)

? ? end_time= time.time()

? ? print("共耗時: %s秒" % round(end_time- start_time, 5))

? ? return result

return wrapper

@clock

def random_sort(n):

return sorted([random.random() for i in range(n)])

if __name__== "__main__":

random_sort(2000000)

輸出結(jié)果:共耗時: 0.65634秒

2. 使用timeit模塊

另一種方法是使用timeit模塊,用來計算平均時間消耗。

執(zhí)行下面的腳本可以運行該模塊。

這里的timing_functions是Python腳本文件名稱。

在輸出的末尾,可以看到以下結(jié)果:4?loops, best of?5:?2.08?sec per loop

這表示測試了4次,平均每次測試重復(fù)5次,最好的測試結(jié)果是2.08秒。

如果不指定測試或重復(fù)次數(shù),默認(rèn)值為10次測試,每次重復(fù)5次。

3. 使用Unix系統(tǒng)中的time命令

然而,裝飾器和timeit都是基于Python的。在外部環(huán)境測試Python時,unix time實用工具就非常有用。

運行time實用工具:

輸出結(jié)果為:

Total?time running random_sort:?1.3931210041?seconds

real?1.49

user?1.40

sys?0.08

第一行來自預(yù)定義的裝飾器,其他三行為:

real表示的是執(zhí)行腳本的總時間

user表示的是執(zhí)行腳本消耗的CPU時間。

sys表示的是執(zhí)行內(nèi)核函數(shù)消耗的時間。

注意:根據(jù)維基百科的定義,內(nèi)核是一個計算機程序,用來管理軟件的輸入輸出,并將其翻譯成CPU和其他計算機中的電子設(shè)備能夠執(zhí)行的數(shù)據(jù)處理指令。

因此,Real執(zhí)行時間和User+Sys執(zhí)行時間的差就是消耗在輸入/輸出和系統(tǒng)執(zhí)行其他任務(wù)時消耗的時間。

4. 使用cProfile模塊

5. 使用line_profiler模塊

6. 使用memory_profiler模塊

7. 使用guppy包

python中怎么判斷函數(shù)是否可以調(diào)用

Python: 測試函數(shù)是否被調(diào)用

# helper class defined elsewhere

class CallLogger(object):

def __init__(self, meth):

self.meth = meth

self.was_called = False

def __call__(self, code=None):

self.meth()

self.was_called = True

然后assert CallLogger的was_called為True就行了。但是這樣的Callable不是個函數(shù):

isinstance(object, types.FunctionType) # Callable will be False

對于這種Callable獲取參數(shù)個數(shù)需要用:

inspect.getargspec(fn.__call__)

怎樣用python3.4編程判斷函數(shù),例如奇偶函數(shù)的判斷,主要是自己能夠任意輸入函數(shù),讓pytho

i?=?input('Input?number:?')

if?int(i)?%?2?==?1:

print('奇數(shù)')

else:

print('偶數(shù)')

如何使用python編寫測試腳本

1)doctest

使用doctest是一種類似于命令行嘗試的方式,用法很簡單,如下

復(fù)制代碼代碼如下:

def f(n):

"""

f(1)

1

f(2)

2

"""

print(n)

if __name__ == '__main__':

import doctest

doctest.testmod()

應(yīng)該來說是足夠簡單了,另外還有一種方式doctest.testfile(filename),就是把命令行的方式放在文件里進行測試。

2)unittest

unittest歷史悠久,最早可以追溯到上世紀(jì)七八十年代了,C++,Java里也都有類似的實現(xiàn),Python里的實現(xiàn)很簡單。

unittest在python里主要的實現(xiàn)方式是TestCase,TestSuite。用法還是例子起步。

復(fù)制代碼代碼如下:

from widget import Widget

import unittest

# 執(zhí)行測試的類

class WidgetTestCase(unittest.TestCase):

def setUp(self):

self.widget = Widget()

def tearDown(self):

self.widget.dispose()

self.widget = None

def testSize(self):

self.assertEqual(self.widget.getSize(), (40, 40))

def testResize(self):

self.widget.resize(100, 100)

self.assertEqual(self.widget.getSize(), (100, 100))

# 測試

if __name__ == "__main__":

# 構(gòu)造測試集

suite = unittest.TestSuite()

suite.addTest(WidgetTestCase("testSize"))

suite.addTest(WidgetTestCase("testResize"))

# 執(zhí)行測試

runner = unittest.TextTestRunner()

runner.run(suite)

簡單的說,1構(gòu)造TestCase(測試用例),其中的setup和teardown負(fù)責(zé)預(yù)處理和善后工作。2構(gòu)造測試集,添加用例3執(zhí)行測試需要說明的是測試方法,在Python中有N多測試函數(shù),主要的有:

TestCase.assert_(expr[, msg])

TestCase.failUnless(expr[, msg])

TestCase.assertTrue(expr[, msg])

TestCase.assertEqual(first, second[, msg])

TestCase.failUnlessEqual(first, second[, msg])

TestCase.assertNotEqual(first, second[, msg])

TestCase.failIfEqual(first, second[, msg])

TestCase.assertAlmostEqual(first, second[, places[, msg]])

TestCase.failUnlessAlmostEqual(first, second[, places[, msg]])

TestCase.assertNotAlmostEqual(first, second[, places[, msg]])

TestCase.failIfAlmostEqual(first, second[, places[, msg]])

TestCase.assertRaises(exception, callable, ...)

TestCase.failUnlessRaises(exception, callable, ...)

TestCase.failIf(expr[, msg])

TestCase.assertFalse(expr[, msg])

TestCase.fail([msg])

Python 幾個重要的內(nèi)置函數(shù)

在學(xué)習(xí)Python的過程中,有幾個比較重要的內(nèi)置函數(shù):help()函數(shù)、dir()函數(shù)、input()與raw_input()函數(shù)、print()函數(shù)、type()函數(shù)。

第一、help()函數(shù)

Help()函數(shù)的參數(shù)分為兩種:如果傳一個字符串做參數(shù)的話,它會自動搜索以這個字符串命名的模塊、方法等;如果傳入的是一個對象,就會顯示這個對象的類型的幫助。比如輸入help(‘print’),它就會尋找以‘print’為名的模塊、類等,找不到就會看到提示信息;而print在Python里是一個保留字,和pass、return同等,而非對象,所以help(print)也會報錯。

第二、dir()函數(shù)

dir()函數(shù)返回任意對象的屬性和方法列表,包含模塊對象、函數(shù)對象、字符串對象、列表對象、字典對象等。盡管查找和導(dǎo)入模塊相對容易,但是記住每個模塊包含什么卻不是這么簡單,您并不希望總是必須查看源代碼來找出答案。Python提供了一種方法,可以使用內(nèi)置的dir()函數(shù)來檢查模塊的內(nèi)容,當(dāng)你為dir()提供一個模塊名的時候,它返回模塊定義的屬性列表。dir()函數(shù)適用于所有對象的類型,包含字符串、整數(shù)、列表、元組、字典、函數(shù)、定制類、類實例和類方法。

第三、input與raw_input函數(shù)

都是用于讀取用戶輸入的,不同的是input()函數(shù)期望用戶輸入的是一個有效的表達式,而raw_input()函數(shù)是將用戶的輸入包裝成一個字符串。

第四、Print()函數(shù)

Print在Python3版本之間是作為Python語句使用的,在Python3里print是作為函數(shù)使用的。

第五、type()函數(shù)

Type()函數(shù)返回任意對象的數(shù)據(jù)類型。在types模塊中列出了可能的數(shù)據(jù)類型,這對于處理多種數(shù)據(jù)類型的函數(shù)非常有用,它通過返回類型對象來做到這一點,可以將這個類型對象與types模塊中定義類型相比較。

分享題目:python檢測周末函數(shù) python星期函數(shù)怎么用
轉(zhuǎn)載來于:http://chinadenli.net/article34/hpeese.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)營銷型網(wǎng)站建設(shè)網(wǎng)站設(shè)計公司網(wǎng)站設(shè)計建站公司搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名