VEX best practice for faster code

   4603   5   4
User Avatar
Member
17 posts
Joined: July 2018
Offline
Hello All,

Over the last half year, I had a go at C# and stumbled upon some different ways to write code that is either more efficient, or compiles down to faster code. (e.g. using For instead of Foreach).

Now returning to Houdini and VEX, I wondered if there were similar optimized ways to write or structure the code? Equally, are there any things that run slowly that should be avoided?

As I am still relatively new to VEX, I am keen to learn best practice, so if anyone has any pointers, they would be gratefully received.

Thanks,


Ash
User Avatar
Member
5277 posts
Joined: Feb. 2012
Offline
Hi Ash,

It depends on a case by case basis. There are some optimizations across the board like using length2/distance2 for comparisons instead of length/distance functions.

Other cases might for example skip the first element in a point cloud look up done on the same input, to skip the point itself, but that's not always true. The most robust approach would be to use removevalue function to remove the point itself by value because the first element is not always guaranteed to be the first element. So in this case the skipping method might be faster but less robust.

There are other tricks like pre-generating points before to use in VEX, rather than create them in VEX.

I show this method here:



There are so many others, and sometimes you have to try each method to figure out the fastest method of all.
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com] https://lnk.bio/animatrix [lnk.bio]
User Avatar
Member
17 posts
Joined: July 2018
Offline
Hi animatrix_,

That is really useful! Separating out the point creation from the manipulation, is a great technique. I'm guilty of trying to stuff everything into one wrangle. Do you have any others like length vs length2 optimisations?

Thanks for sharing, as always!


Ash
Edited by ashcic - Nov. 17, 2021 04:54:20
User Avatar
Member
5277 posts
Joined: Feb. 2012
Offline
ashcic
Hi animatrix_,

That is really useful! Separating out the point creation from the manipulation, is a great technique. I'm guilty of trying to stuff everything into one wrangle. Do you have any others like length vs length2 optimisations?

Thanks for sharing, as always!


Ash

Oh yes trying to do everything in a single wrangle can cost serious performance losses sometimes depending on the algorithm, which is often a topic of brevity vs clarity.

For example when computing something like PCA (Principal Component Analysis), one might be inclined to do everything using a single Detail Wrangle node.

What I found that maximizes performance is to separate out each section of the algorithm where it makes sense, so the parts that can be parallellized are parallellized. For example the part where you have to compute the center of the data set can be done using a Point Wrangle in parallel.

Especially after compiling the entire chain of Attribute Wrangle nodes, it will cook like a single Wrangle node, while maximizing the performance for each part of the algorithm as best as it could.
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com] https://lnk.bio/animatrix [lnk.bio]
User Avatar
Member
17 posts
Joined: July 2018
Offline
Thanks again animatrix_!

Your advice is so helpful. I'm guessing that a lot of your insights came through experimentation and understanding of computation in general. I think I'll have to rack up the hours experimenting.
User Avatar
Member
5277 posts
Joined: Feb. 2012
Offline
Yes and be sure to check out these free chapters at least to see more of the same tips and tricks:

https://www.sidefx.com/learn/collections/pragmatic-vex-volume-1 [www.sidefx.com]
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com] https://lnk.bio/animatrix [lnk.bio]
  • Quick Links