being a long time C++ developer I just started working on algorithms in Python. I'm currently profiling my code to get a feel for how to program efficiently in Python. One thing in particular stood out to me for which I would be very glad to get an expert explanation.
I wrote this wrapper function for a ray-triangle intersection:
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
When profiling (using cProfile) code that executes this function a lot of times I have the following result:
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}
How come this wrapper adds so much overhead? The only thing I can really see going on here is the array element access. Coming from C++ this really confuses me :D
Any help on this would be super appreciated. I want to get my algorithms as fast as possible.
Thanks in advance! Cheers!