Skip to Main Content
高效能PYTHON程式設計
book

高效能PYTHON程式設計

by Micha Gorelick, Ian Ozsvald
August 2015
Intermediate to advanced content levelIntermediate to advanced
384 pages
7h 42m
Chinese
GoTop Information, Inc.
Content preview from 高效能PYTHON程式設計
108
|
第六章
# 從初始條件演進
start = time.time()
for i in range(num_iterations):
grid = evolve(grid, 0.1)
return time.time() - start
❶
這裡使用的初始條件與圖 6-2 的方形範例相同。
dt
與格點元素的值已經被選擇得足夠小,好讓演算法保持穩定。關於這個演算法之收斂
特性的較深入探討,請參閱 William Press 等人所著的《
Numerical Recipes
》(第三版)
http://www.nr.com/
)。
配置太多的問題
透過在純 Python 演進函式上使用
line_profiler
,我們可以開始瞭解緩慢執行時間的可
能原因。檢視範例 6-5 的效能分析器輸出,我們看到函式的大多數處理時間都耗費在
微分的計算及格點的更新
1
,這是我們期待的結果,因為這純粹是計算密集型(CPU-
bound)問題 任何不是花在解決計算密集型問題的時間都是要最佳化的地方。
範例
6-5 
Python 2D
擴散效能分析
$ kernprof.py -lv diffusion_python.py
Wrote profile results to diffusion_python.py.lprof
Timer unit: 1e-06 s
File: diffusion_python.py
Function: evolve at line 8
Total time: 16.1398 s
Line # Hits ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

流畅的Python

流畅的Python

Luciano Ramalho
手把手教会你linux

手把手教会你linux

桑德.范.乌格特

Publisher Resources

ISBN: 9789863477105