If() and if..then speed differences

I started writing an explanation, then I read it, I didn’t like what I wrote, it seemed a bit pedantic. So I just erased it and made a humoresque “ehr… forget it”. It could be ok if someone did not add “extra comments” to it.

Ab uno disce omnes

Not sure if there is anything else to add other then what Joe already explained (he should know because he was the former compiler expert at Xojo).
Tims input was … heu… a bit ‘off’ track. :slight_smile: ( but with good intentions of course).

This will avoid the cost of converting to a string, but replace it with the cost of a string concatenation. The Xojo framework should recognize that it can just return the original string as-is, but it still means an extra function call at runtime.

That’s still cheaper than a runtime Text → String conversion, but using an explicitly typed constant or CType would be the more efficient route.

5 Likes

Yes, it’s useful to know that CType is the preferred way.

1 Like

Still on the topic of text and speed. If I need to do a lot of searching and matching on strings of various lengths (mostly word lengths around 2 to 10 characters), would it be faster to convert string input to the ASCII Digits and work with numbers until done, then convert back to strings for display. I figure loop iterations would be in the hundreds to thousands.

In the olden days, and numeric comparison was faster than a string compare. So if I were doing a lot, I’m wondering if it would be faster to pay for the conversion overhead to numbers and back and do the rest of the work with numerics?

What do you mean by ‘ASCII digits’?

I assume that you mean that you would be converting A to 65; B to 66 etc. So perhaps instead of text now you have a huge array of integers. I am not sure how you would set up your search, and I cannot imagine that it could be done in a way that was faster than using searches for strings inside of text. The latter must be highly optimized in virtually any common language because this is such a common use case.

I have no specific knowledge on the subject so would be interested to hear anything less speculative.

Yes, I mean converting the letter to its number; i.e A = 65. So if I have the word, “CAT”, I can represent that as 676584. And if I have a text string of say 150 characters, and want to know if it contains CAT, I can “trim” off six digits at a time and compare to 676585. But in describing it just now, I can imagine any “trim” or “take from the middle” type function expects to work with text. So I might as well stick with text/strings from the beginning.

1 Like

I think that is what instrb used to do, it’s wickedly fast. https://documentation.xojo.com/api/deprecated/instr.htmlB

1 Like

How does IndexOfBytes compare?