Grasshopper

algorithmic modeling for Rhino

Profiling Vector Multiplication

Have been profiling code to find the sticky parts using David's advice on StopWatch here.

Have come to realize that:

MyVector3d  *= MyDouble

is about 10 times slower than

MyVector3d = new Vector3d(MyVector3d.X * MyDouble, MyVector3d.Y * MyDouble, MyVector3d.Z * MyDouble)

Has anyone else come to that conclusion or know why it happens?  Are some vectors slow with some operators?  Are those funny operators (+=, *= etc) slow in general?  That would be sad because I love those.

daniel

Views: 156

Replies to This Discussion

I get a speed-difference of about two-and-a-half, not 10. If you call the operator * then it involves an extra function call as opposed to using the constructor directly. *= will involve an extra function call and an extra assignment. This may explain where the slower performance comes from, especially since vector multiplication is such a fast method to begin with.

I can do 100 million operations in 0.37 seconds using the constructor and 0.85 seconds using the *= operator. Are you just surprised by this, or do you have code that is not running fast enough?

--

David Rutten

david@mcneel.com

Thanks, that's interesting.  It was code that wasn't running fast enough - a kind of swarm with vectors between every node.  It was a solid factor of 10 for me, 30ms vs. 300ms.  Running plenty fast now.  Also increased speed by about 4 by separating out the line with multiple operations.  Like:

original, running at ~1200ms:

MyVector *= MyDouble * MyArray(i,j)

and the much faster version, running at ~30ms:

MyArrayValue = MyArray(i,j)

MyVector = New Vector3d(MyVector.X * MyDouble .... etc)

MyVector = New Vector3d(MyVector.X * MyArrayValue .... etc)

It's a shame the latter is so much faster because I've always liked keeping the code compact.  It is certainly more readable this way though.

daniel

Shame indeed. I'd expected the .NET compiler people to have optimized operators like this, but it seems it is significantly slower. Anyway, glad you managed to speed things up.

--

David Rutten

david@mcneel.com