Vectorization Function Performance Comparison
- October 29, 2024
- 1 reply
- 1993 views
Following
Note that I use the term "vectorize" in the mathematical sense of "convert a matrix to a vector" rather than the programming sense of carrying out operations in parallel (e.g., Mathcad's vectorize operator).
The suggested function Range2Vec is similar to one I usually use (vec) except that instead of stack it uses indexing to add new elements to the constructed vector. I vaguely recall that stacking is inefficient time-wise compared to indexing, so I thought it would be interesting to see how the two variants compared.
Then I thought, "Hey, why stop at two mousetraps? Let's build some more to see if anything hits paydirt".
So, I played with a few variants and concluded yet again that stacking is probably slower than indexing. However, there isn't much objectively in it even for a few million elements, even in nested arrays.
Note that most of the variants tested fully convert nested arrays to vectors, whilst Range2Vec and vec` do not - they're only targeted at vectorizing the first level of an array.
Take the results with several pinches of salt. The times depend upon what else your computer is doing. In the example below Range2Vec is shown to perform worse than the rest. This is very unusual. Range2Vec holds Gold or Silver during most runs, as it should, given it doesn't have to have code to deal with nesting. . Besides, the worksheet's probably full of bugs, anyway. 🙂
Having said that, the times are reasonably consistent in showing "bands" of times. The slowest two functions are usually vec (uses stack to add elements) and vec3 (uses trim rather than submatrix to remove the dummy empty array values).

Stuart
References:
https://community.ptc.com/t5/Mathcad/Exclude-some-values/m-p/980366/highlight/true#M214627

