2010-06-29 105 views

回答

11

最好的方法是運行一些benchmark tests(測試單個功能)或Profiling(測試整個應用程序/程序)。 Python帶有內置的Profiler。

或者,您只需在程序開始處設置開始時間,並在程序結束時從開始時間減去當前時間,即可返回到the very basics。這基本上是非常簡單的基準測試。

下面是從an answer從鏈接問題的實現:

import time 
start = time.time() 
do_long_code() 
print "it took", time.time() - start, "seconds." 

Python有納入其標準庫something for benchmarking,也是如此。

從示例給頁面上:

def test(): 
    "Time me" 
    L = [] 
    for i in range(100): 
     L.append(i) 

if __name__=='__main__': 
    from timeit import Timer 
    t = Timer("test()", "from __main__ import test") 
    print t.timeit() 
+1

+1分析 – 2010-06-29 21:33:10

3

使用Profiler!

python -m cProfile -o prof yourscript.py 
runsnake prof 

runsnake是查看性能分析輸出的好工具。你當然可以使用其他工具。

更多關於這裏的探查:http://docs.python.org/library/profile.html

+0

「CPython的」 應爲 「CPROFILE」。 – 2010-12-08 22:48:02