Correct syntax for using And Or in If Else statements

Bear in mind that most class-based method calls are resolved at runtime, not compile time. I know that things like Ubound are optimized, so the overhead in a loop is low. I would expect Length to be highly optimized as well.

2 Likes

It’s always been best that you don’t use Ubound (LastRowIndex) within your loop because it does recount on each iteration. I just did a simple test on iterating over 1,000,000 rows. Caching the maximum value was faster through the loop by 0.002 to 0.008 seconds over 10 tests. If you need speed, cache your loop maximum.

EDIT: Using a data set of 10,000,000 rows shows that caching the maximum is faster by a margin of 0.01 to 0.05 seconds over 10 tests.

1 Like

Perhaps off-topic (apologies), but hopefully interesting to some as we are talking about optimizations. 2 decades ago my specialty as an SAP ABAP developer was performance optimization. One customer running IBM DB/2 in the retail area had severe performance issues.

We started to use so-called “database hints”. These database hints are like database-specific SQL injections mainly to “guide” the database optimizer. Let’s say that the database optimizer is using the foreign key xy but you know exactly that it is wrong and that the foreign key zw is the better choice, so you were able with database SQL hints to force the database to use your chosen foreign key. Or you noticed that your database optimizer regularly started an SQL run and after 30% switched to a full table scan. In this case, it was better (aka faster) to force the database to start immediately with a full table scan and bypassing the database optimizer. After a few months of work, the performance of IBM DB/2 was again outstanding and the customer happy.

A few years later the customer (general management decided solely based on price) decided to switch to ORACLE. Bad thing: none of the internal developers nor the ORACLE salespeople had noticed the intensive use of database hints. The customer migrated but the system was unbelievable slow. So they ultimately came back to us and we started from scratch converting the DB / 2 hints to Oracle hints :-). Good for our business at that time, but it clearly shows how optimizations can be very tricky. And what is working well in one release, might look completely different in another release, on another platform or after any update … and you clearly depend on the manufacturer telling you what is best. Your own testing will help you on a specific release, but with every update, there is a certain risk that the results will look entirely different if the manufacturer changed something under the hood. Disclaimer: I don’t expect that the Xojo logic under the hood will change any time soon, but you never know.

1 Like

Maybe not in this case, but it has certainly happened in the past where performance characteristics change under the hood and suddenly your clever optimizations become a liability.

1 Like

Update: I just noted that it should of course read “Full Table Scan” and not “Full database scan” in my post, which I can’t edit any longer ;-).