algorithmic modeling for Rhino
I'm trying to speed up a boids type simulation by minimizing the number sqrt calculations involved with distances, vector lengths, and unitizing. What I'm wondering about is if the length property of a vector3d is something that is calculated only when called upon or if it's calculated up front at instantiation and just retrieved when called upon.
it is not cached. But, are you absolutely certain that a lot of time is spend in Sqrt? It seems like a micro-optimization that's not going to help you much in the end.
Thanks for the wisdom. I had heard that sqrt stuff can slow things down with large number of agents and iterations. But then again this was from the game dev community so their idea of large is probably much bigger than mine. But if vector lengths aren't cached I'll try running distance/length checks with dist ^ 2 instead and see if it makes any difference.
minimizing sqrt can account for a sensible speed improvement in a boids-type simulation, especially if the assembly is compiled in release mode. This is still a small-scale optimization, though.
See the attached definition for a simple example.
McNeel Europe, Barcelona
I didn't even realize that squaredlength was an existing property. Pleasant surprise.
Looks like it does a reasonable job at speeding things up though. At 500 agents it's running a little over 3x faster.