是一個很長時間的C++開發人員我剛開始着手Python中的算法。我目前正在分析我的代碼,以瞭解如何在Python中高效編程。有一件事特別突出,我很樂意得到專家的解釋。Python元素訪問性能
我寫此包裝函數的光線 - 三角形相交:
def rayIntersectsTriangle(origin , direction , meshData , poly , worldCoordinateVertices):
return mathutils.geometry.intersect_ray_tri(worldCoordinateVertices[ meshData.loops[ poly.loop_start ].vertex_index ],
worldCoordinateVertices[ meshData.loops[ poly.loop_start + 1 ].vertex_index ],
worldCoordinateVertices[ meshData.loops[ poly.loop_start + 2 ].vertex_index ],
direction , origin) != None
如果在分析(使用CPROFILE)執行該功能有很多次代碼我有以下的結果:
ncalls tottime percall cumtime percall filename:lineno(function)
15694126 22.314 0.000 25.812 0.000 ****.py:176(rayIntersectsPoly)
[...]
15694126 3.497 0.000 3.497 0.000 {built-in method mathutils.geometry.intersect_ray_tri}
這個包裝怎麼會增加這麼多的開銷?我唯一能看到的是數組元素訪問。來自C++這真讓我困惑:D
任何對此的幫助將超級讚賞。我想盡可能快地獲得我的算法。
在此先感謝!乾杯!
請參閱https://wiki.python.org/moin/TimeComplexity –
......順便說一句,如果以數學爲中心的工作負載的運行時性能是您的首要目標,那麼Python可能不是您的語言。我可能會建議[Julia](https://julialang.org/)或[Go](https://golang.org/)? (Julia的網頁涵蓋了這兩個基準)。 –
太棒了。感謝您的資源。我目前正在爲Blender開發一個插件,所以我沒有替代Python的 – Marcel