I found another laggy internal routine to compare,
StringToInt. Admittedly, there's likely a more efficient method than the one I came up with for it , but the internal function came out well ahead in this one also.
I did find out one interesting thing doing this comparison though, in the case of a mixed string, numbers and letters, it reads it from left to right stopping at the first non-number. Mine reads right to left stopping at the first non-number.
The scripts I used for comparison:
The main function caller:
void main()
{
int i;
for (i; i < 250; i++)
{
ExecuteScript ("test_s2i_bio", OBJECT_SELF);
ExecuteScript ("test_s2i_1", OBJECT_SELF);
}
}
void main()
{
int nVal = StringToInt ("889adfiy224");
}
"test_s2i_1"
int _StringToInt(string sString)
{
float fVal;
int nTest, i;
int nLength = GetStringLength (sString) - 1;
for (i; i <= nLength; i++)
{
nTest = FindSubString ("0123456789", GetSubString (sString, nLength - i, 1));
if (nTest == -1) return FloatToInt (fVal);
else fVal += IntToFloat (nTest) * pow(10.0, IntToFloat (i));
}
return FloatToInt (fVal);
}
void main()
{
int nVal = _StringToInt ("889adfiy224");
}
and the results:
string - "889adfiy224"
test_s2i_bio 5000 - 606
test_s2i_1 5000 - 894
test_s2i_bio 5000 - 439
test_s2i_1 5000 - 918
string - "1243890734"
test_s2i_bio 5000 - 580
test_s2i_1 5000 - 1419
Based on those results, I think the internal function must check left to right through the whole string, find the spot the numbers stop, then work backwards converting it to a number.
Otherwise the signifigant increase in time on mine (to be expected, more operations == more time) with the extra numbers compared to the comparable numbers in the internal routine doesn't make sense.
It does look like The.Gray.Fox was right though, even a slow internal routine is faster than an external one. They might be comparable in some scenarios, but overall not so much so.
That's a bit disheartening to me, since I was considering rewriting the whole spell engine for my next scripting project. It looks like there's be an extra few fractions of a millisecond for every internal function replaced to contend with in any sort of large scripted system overhaul.