This routine that creates the HTML table at the end of the preceding section concatenates the entire HTML string into the string variable s, then writes the contents of that variable to the browser by using the Response.Write method. You could make the routine write the contents directly, by substituting Response .Write wherever the code has s = s &. If you do that in a WebClass, you'll slow down the code considerably. In fact, for long tables, you'll get a significant increase in speed by creating a temporary string for each row, then concatenating them once at the end of the display. There are two reasons.
First, you should avoid making unnecessary object references whenever possible. A good rule of thumb is that if you're going to use an object more than once in a routine, create a local variable reference. Similarly, if you need to access a property of an object more than once in a routine, create a local variable and copy the property into it. Calling the Response.Write method multiple times forces VB to make unnecessary object reference calls. This routine could be optimized even more by using the With…End With structure to avoid referencing the recordset variable R as often as it does.
Second, the routine is slow because VB must allocate memory and create a new string every time you concatenate two strings. The longer the two strings are, the more memory (and time) it takes to concatenate them. By using shorter strings or byte arrays, you can often dramatically speed up a routine with little work.
If you want to see how much you can speed up a routine, create a new standard .exe and paste the routines in Listing 4.3 into the form; then run the project. You can find the code in the StringConcatenation project on the Sybex Web site.
Note
To download code, navigate to http://www.sybex.com. Click Catalog and search for this book's title. Click the Downloads button and accept the licensing agreement. Accepting the agreement grants you access to the downloads page for the book.
Each function in the project creates a string that contains the letters of the alphabet repeated 10,000 times. The longConCat function appends the alphabet string to a single string variable 10,000 times in a loop. On my computer, an NT-based 400Mhz Pentium II running this program in the development environment, the longConCat function takes about 17 seconds. The shortConCat function appends 10 alphabet strings together, then appends the resulting string to another string 1,000 times. Breaking the string into 1,000 shorter strings decreases the time dramatically—the shortConCat function takes less than 3 seconds. The reallyShortConCat function takes only about .3 seconds. It appends 100 alphabet strings together, then appends the result to another string 100 times. The result shows that fewer, larger concatenations are more efficient than many smaller ones. The byteConcat function is even more efficient—it takes only .15 seconds. That's a speed improvement of 11,333% (no, that's not a misprint) without any special API calls. When you compile the project with all compiler optimizations turned on, the difference is even more dramatic. Sometimes small changes make a big difference.
Imagine the difference in load on your server when you're serving large tables to many users! Although routine efficiency isn't the main theme of this book, you should carefully consider what effect your routines will have on the complete application—especially under a heavy multiuser load. This whole discussion of efficiency points out nicely why Microsoft has added WebClasses to VB. The increase in efficiency of a compiled application over similar code in interpreted mode isn't as high as you might expect, but the increase in efficiency of strongly typed code is considerable and can be well worth the increased complexity of installing and updating a VB application compared to a set of text-file ASP pages.