Skip to main content
23-Emerald V
October 29, 2024
Question

Vectorization Function Performance Comparison

  • October 29, 2024
  • 1 reply
  • 1993 views

Following @DJNewman 's message on the topic of implicit vectorization of range variable being removed from Mathcad Prime 10, I looked at the referenced support article CS416474.   The resolution to the "problem" was to use a range-to-vector conversion program.

 

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).

 

2024 10 29 F.png

 

Stuart

 

References:

 

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

 

Article - CS416474 : Inline evaluation to convert range into vector worked in Mathcad Prime 1.0-9.0, but now fails in Mathcad Prime 10.0

 

https://en.wikipedia.org/wiki/Vectorization_(mathematics)

1 reply

DJNewman
18-Opal
October 29, 2024

Something to (possibly, no promises) think about for Mathcad Prime 11: We're strongly pushing R&D to put in a built-in vec() function that should also do this. So you may wish to repeat your testing once that's out. (If it happens.)

PTC Marketer for Creo and Mathcad. I run their YouTube channels, some Creo campaigns, and all Mathcad campaigns and communications.
23-Emerald V
October 30, 2024

That sounds great, Dave. I certainly look forward to doing that round of tests again.

 

If it is implemented, please consider my comments about naming the function. The naming style for other functions that create and manipulate arrays tends to be short, lowercase, and meaningful. Thank you.

 

As an aside, will the function be limited in scope or capable of vectorizing any type of array (including arbitrarily-deeply nested ones)?  As you (PTC) can write functions that take optional arguments, perhaps a depth limit could be incorporated - there are occasions when it's only required to go so far down a data structure.  Here's a quick and brutal version of vec that takes a (non-optional) depth argument.

 

vec7 definition:

2024 10 30 B.png

 

vec7 applied to a slightly nested data structure

 

2024 10 30 A.png

 

Oh, as it's a closely related issue, could you please ask the development team to include an IsRange function (that doesn't evaluate a range if evaluated rather than used in a definition!).

 

Stuart