You could write your own std deviation ClassMethod() in ObjectScript. You then have the flexibility to use that however you want.

The logic doesn't look that complex, and for a few thousand rows would run fast enough in Object Script.

See link below for the calculations you need to do:

https://www.scribbr.com/statistics/standard-deviation/

You can validate your results with the STDDEV(SQL) method, which will also allow you to compare performance.

For me, if I was faced with 2 possible solutions and was concerned about performance, I would simply test both solutions within a loop and time how long it takes to do n number of loops.

Something like this

Class Performance.Test [ Abstract ]
{ ClassMethod SolutionA() As %Status
{
 
    set x=9
    quit $$$OK
} ClassMethod SolutionB() As %Integer
{
 
    set x=9
    quit 1
} ClassMethod TestSolutions()
{
 
    write "SolutionA Start : "_$zdatetime($ztimestamp,3,1,3),!
    for i=1:1:90000000 {
        set ret=..SolutionA()
    }
    write "SolutionA End : "_$zdatetime($ztimestamp,3,1,3),!
    
    write "SolutionB Start : "_$zdatetime($ztimestamp,3,1,3),!
    for i=1:1:90000000 {
        set ret=..SolutionB()
    }
    write "SolutionB End : "_$zdatetime($ztimestamp,3,1,3),!
} }

For quick and dirty logging I use:

kill ^Log at the beginning of the ClassMethod/Routine

Then use the following in your code to track what is going on:

set ^Log($increment(^Log))="Your message goes here. For example VariableX="_VariableX

Run your REST API and then take a look at ^Log.

You can either put lots of "set ^Log..." statements in, or just a few to narrow down where the issue is and just keep adding them till you get to the code in question. This might take a few iterations, but it generally works for me.

Thanks for the clarification.

The answers from @Manel Trèmols and @Ramil TK should both work for you.

You might have to do some additional formatting depending on what datePart you getting the difference in. For example if you get the difference in Minutes ("n"), then you will have to convert that into Days, Hours, Minutes etc, so it will depend on what you are expecting in the data and how big those differences might be.