MAXScript FAQ > How To Make It Faster > Optimizing Particle Flow Script Operator For Loops For Speed |
but is 4 times slower. When calculating inside a Particle Flow operator, you want the FASTEST method because the code is called thousands and even millions of times, once for each particle.
The first code block evaluates in 3360 ms when run on a particular machine.
but the same code using the length() method
evaluates in just 813 ms on the same machine, which is 4.13 times faster!
In addition, assigning to a temporary variable pvel adds 200 ms to 1 million calls in both cases, so rewriting the original code block to
without any intermediate variables could shave off up to 600 ms per one million calls on top of the speed up from using the length() method.
Here is another test to simulate this - it contains 3 variable assignments within the loop and the original velocity calculation:
The above code is very similar to what the original code was doing (minus actually reading and writing particle values). It evaluates in 5750 ms.
...executes in 3200 ms just because we got rid of the 3 additions and 3 multiplications within the MAXScript code - length() does the same internally, but is implemented in C++ and is thus much faster.
The inline version of the test code without any intermediate variables which calculates the same value at the end needs only 1400 ms!
In short, avoid using local intermediate variables within your PFlow scripts and use built-in methods to get the best performance, as in