OK, this if for the newbies to REALbasic out there. Here are some common tricks for speeding up your code. I have listed some of the most common tricks I have seen and used. If anybody else any other tricks that they would like to share, please feel free to share them. An old dog can learn new tricks!
------------------------ Don't put a computed expression in a the head statement of a for-next loop. Don't use... for i = 0 to getFileCount next but instead use... count = getGetFileCount for i = 0 to count next The for-next statement will call the getFileCount method (or compute the expression "count + 1" in the expression for i = 0 to (count + 1)) each and every time around the loop. ------------------------ When concatenating a lot of strings use a string array and a join statement instead of the "+" operator. Don't use... dim s, s1 as string for i = 0 to 10000 s = s + s1 next but instead use... dim s, s1 as string dim sa() as string for i = 0 to 10000 sa.append(s1) next s = join(sa, "") This is nearly INFINITELY faster!!! One caveat, if you are concatenating a few strings then the "+" operator is the way to go. What the crossover point is for when the join method out computes the "+" operator is unknown to me. Just use common sense when applying this technique. ------------------------ - Use #pragmas to speed up code execution. #pragma DisableBackgroundTasks DisableBackgroundTasks will give you the most bang for the buck since it keeps RB from processing other threads and the overhead for doing this can be expensive. A good place to use this is in tight loops doing computationally expensive operations. The other #pragma statements can speed things up, but are more dangerous to use if your code is not fully tested. ------------------------ Under Mac OS X, make the listbox invisible when adding a lot of items. This bypasses the drawing routines in the listbox and OS X's system wide double buffering will keep the listbox visible while you do the updates. On other systems that don't have system double buffering, you will get a flicker. ------------------------ Cache deleted objects. Allocation and deletion of objects can be expensive to do. When you are done with an object, store it in a cache and then check the cache when you want that type of object again. The use of this technique depends on the usage pattern of the target objects. If your application has the pattern of allocating a lot of objects and then deleting them all in a yo-yo type pattern, then this technique will help. Be careful to de-initialize the deleted object in order to prevent side effects. ------------------------ When drawing to a canvas insert the extra logic needed to compute what is visible on the screen and only draw that which is visible. If you are doing a canvas based control take the time to compute what objects will be visible on the screen. Don't just blindly draw all of your data structures to the screen and rely on implicit clipping. The extra code you insert will more than be made up in execution time since drawing operation are expensive in comparison to computational comparisons. I have seen HUGE speed ups using this technique. _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives of this list here: <http://support.realsoftware.com/listarchives/lists.html>
