I recently created a new function to display some data on a chart, which seemed to be doing what I wanted, but was taking >3seconds between screen refreshes (Tools, Preferences, Miscellaneous, Display chart timing).
On looking at “Code Check & Profile”, the script was executing a number of nested functions many times - I changed the code so that those functions only executed once, and showed a big improvement, but still quite slow.
Looking at the execution time again, showed that “array operator ^” (exponentiation operator) was taking most of the processing time. So I designed the following experiment:
//
function mthExponent(inpArray, inpExponent)
/*
Description: this function returns an array raised to the power of the exponent.
Parameters:
inpArray: the "base"
inpExponent: the "exponent"
Refer to Wikipedia for a more complete explanation of exponentiation.
*/
{
return inpArray ^ inpExponent ;
}
//
function mthPower(inpArray, inpExponent, inpUseDefault)
/*
Description: this function returns an array raised to the power of the exponent.
Parameters:
inpArray: the "base"
inpExponent: the "exponent"
inpUseDefault: use AB's default exponetiation operator
Refer to Wikipedia for a more complete explanation of exponentiation.
*/
{
// Use the default operator, or the user has specified an more complex exponenet
if (inpUseDefault OR (frac(inpExponent) > 0) OR (inpExponent < 0))
// This code executes in 10.23 ms for 100 iterations
result = inpArray ^ inpExponent ;
else
// The user has specified a simple positive integer exponent
{ // This code executes in 0.421 ms for 100 iterations
result = inpArray ;
if (inpExponent > 1)
{
for (loop = 2; loop <= inpExponent; loop ++)
{
result = result * inpArray ;
}
}
}
return result ;
}
// Create some random data
baseData = mtRandomA(123) ;
// How many times to do each test
cnsNumIters = 100 ;
// Test: default exponentiation operator
for (loop = 0; loop < cnsNumIters; loop ++)
testDefA = baseData ^ 2 ;
// Test: default exponentiation operator via functions -> timing
for (loop = 0; loop < cnsNumIters; loop ++)
testExpA = mthExponent(baseData, 2) ;
doDefault = False ;
if (doDefault)
{
// Test: default exponentiation operator via function -> timing
for (loop = 0; loop < cnsNumIters; loop ++)
testPowA = mthPower(baseData, 2, True) ; // Uses default exponentiation operator
}
else
{
// Test: multiplicative exponentiation via function -> timing
for (loop = 0; loop < cnsNumIters; loop ++)
testPowB = mthPower(baseData, 2, False) ; // Uses multiplication
}
// Sameness?
isSame = AlmostEqual(testDefA, testExpA) ;
if (doDefault)
isSame = AlmostEqual(testDefA, testPowA) ;
else
isSame = AlmostEqual(testDefA, testPowB) ;
// Some other test, stops debug mode from obliterating values in "Watch" pane.
zzzz = 1 + 2 + 3 + 4 + 5 ;
, and here’s the profile for it:
Does anyone else have similar experience or an explanation?