cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

Translate the entire conversation x

Mathcad Express Challenge - faster recursive chr2str function

StuartBruff
23-Emerald IV

Mathcad Express Challenge - faster recursive chr2str function

My string library defines three different representations of a string, two of which should already be familar: the string and the vector of character codes. The third type is the vector of characters.

 

In the following, "str" indicates a string, "vec" indicates a vector of character codes, and "chr" represents a vector of characters. For example, chr2str indicates a conversion from a string to a vector of characters.

 

chr2str itself is relatively quick, even for long strings. chr2str0 has the limitation of hitting Mathcad's recursion depth limit. chr2str2's use of folding over concat, however, is proving somewhat slow once the string length increases to above about 20,000.

 

The challenge is to define a faster recursive variant of chr2str.

 

2025 10 08 D.png

 

Where the dependencies are as shown below (except the timer functions, which are in an Area at the top of the attached Mathcad Express 11 worksheet).

 

2025 10 08 C.png

 

Stuart

2 REPLIES 2
Werner_E
25-Diamond I
(To:StuartBruff)

Why are you unhappy with your chr2str function? Its fast and does not suffer from the silly recursion limit.

You may speed up chr2str2 a little bit by replacing the call to chr2str2 by the fold command directly.  But speed in no way comes near that of chr2str.

Werner_E_3-1760023561183.png

 

Furthermore chr2str0 and chr2str2 not only are slower, they suffer from another drawback as well:

Werner_E_1-1760023136744.png

 

 

StuartBruff
23-Emerald IV
(To:Werner_E)


@Werner_E wrote:

Why are you unhappy with your chr2str function? Its fast and does not suffer from the silly recursion limit.

You may speed up chr2str2 a little bit by replacing the call to chr2str2 by the fold command directly.  But speed in no way comes near that of chr2str.

Werner_E_3-1760023561183.png

 

Furthermore chr2str0 and chr2str2 not only are slower, they suffer from another drawback as well:

Werner_E_1-1760023136744.png


I'm not unhappy with chr2str, it's the performance of chr2str2 that leaves me feeling unfulfilled.  

 

And that slowness is why the specific challenge.  Is there a way of improving the performance of the recursive algorithm?  Might the underlying reason for any improvement be more generally applicable?  Even a small improvement might take a function's performance from annoying to bearable. 

 

It can also be surprising just what a difference an apparently small change can make to a function's performance.   For example, the marked increase in speed by using update1 rather than stack in my vec function (as mentioned in another thread and as shown below).

 

Even some of the matrix summation functions surprised me with how quick they were - look at matsum.sigrow and matsum.onesmat below, especially the latter when compared to the other Sigma operator (S) variants.

 

Summation Function Timings:

2025 10 09 C.png

 

Summation Function Definitions:

2025 10 09 D.png

 

Auxiliary Function Definitions:

2025 10 09 E.png

 

Stuart

 

Mathcad Prime (Express) 11 worksheet attached.

 

Announcements

Top Tags