Skip to content

Guest Post: A Few Little Xojo Code Performance Tips by Eric Gibbon

Eric Gibbon has been using Xojo to develop bespoke cross-platform applications for Mac and PC and for the web for 15 years. He lives in Stamford, England, and is an active member of the Xojo UK User Group.

There’s an old programmer’€™s saying: If it works, leave it alone.€ But sometimes we have to go back to code that works to make it go faster, because it’€™s too slow.

Over time I have picked up some tips on how to get better performance from Xojo code. I have used these tips to improve old code running on all platforms and have seen big improvements. They are quick and easy to do.

If it works, leave it alone.

Removing Unnecessary Loop Calculations

Loops are used to repeat blocks of code over and over again. If we can reduce inefficiencies in loop code, we can make significant performance improvements. Take the following example, which may look familiar:

// Also works with MyPopupMenu.ListCount-1
For i As Integer = 0 to MyListbox.ListCount - 1

Here the For loop is calculating the value of (MyListbox.ListCount – 1) each time it goes around the loop. If our Listbox had 5,000 rows, the loop would calculate this same value 5,000 times! We get improved performance by calculating it only once:

Dim n As Integer = MyListbox.ListCount - 1 // or MyPopupMenu.ListCount-1 
For i As Integer = 0 to n

Flattening Code

Xojo is great for building structured code using methods, but we can speed things up in a loop by reducing the number of calls to methods. In our tiny example above, the CalculateTotals method gets called 5,000 times. If the code within the CalculateTotals method were put directly into our loop instead, 5,000 calls to the method would be eliminated, so the loop code would run faster. If you have a feature that processes a large volume of data, and various different methods get called from inside a loop, flattening the code would help improve performance.

Adding Rows in Listboxes

I often see code like this to add rows to Listboxes:

MyListbox.AddRow " "
MyListbox.Cell(MyListbox.LastIndex,0) = w
MyListbox.Cell(MyListbox.LastIndex,1) = x
MyListbox.Cell(MyListbox.LastIndex,2) = y
MyListbox.Cell(MyListbox.LastIndex,3) = z

This is inefficient and can be improved by adding the entire row to the list in one go:

MyListbox.AddRow w, x, y, z

This works significantly faster and is less code to write. If your lists are long and have several columns you will see a difference. For a WebListbox on a WebPage it is a very noticeable difference. And if you don’t want long and hard to read lines of code like this…

MyListbox.AddRow rs.Field(“value1”).StringValue, _
  rs.Field(“value2”).StringValue), rs.Field(“value3”).StringValue), _

…you can store values in variables first:

Loops are used to repeat blocks of code over and over again. If we can reduce inefficiencies in loop code, we can make significant performance improvements. Take the following example, which may look familiar:

Dim c0 As String = rs.Field(“value1”).StringValue
Dim c1 As String = rs.Field(“value2”).StringValue
Dim c2 As String = rs.Field(“value3”).StringValue
Dim c3 As String = rs.Field(“value4”).StringValue

MyListbox.AddRow c0, c1, c2, c3

This makes the code easier to read and edit. Although it is not as fast as the single line of code to add the row, it is still much faster than adding a blank row and then populating each cell one by one.

Making Listboxes Invisible

Another tip is to make a Listbox invisible while rows are being added, and visible again when it is ready. This is not necessary for most uses, but if you are populating a Listbox with a very large amount of data, this can bring a big performance improvement.

To make sure there isn’t a blank space in the UI while it is invisible, an identical blank Listbox can be put behind the Listbox in use, or a “please wait”€ image can be used instead, but you have to remember that the window may be resizable and the list may be resized too.

Use the Xojo Profiler

And don’t forget to use the Xojo Profiler to check your code performance when you see it needs some work. First work on the functions that take the longest, and then look at commonly used methods to see if you can speed them up using these tips.

Remember, small performance improvements here and there can mean large improvements to your app responsiveness overall.

Happy coding!


Listen to the XojoTalk Podcast with Eric & Paul and read this other post from Eric on Xojo Networking.