Page #36 (Chapter 4 - Introduction to WebClasses)

Chapter 4 - Introduction to WebClasses

Visual Basic Developers Guide to ASP and IIS
A. Russell Jones
  Copyright 1999 SYBEX Inc.

Optimize, Optimize, Optimize
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.
Listing 4.3: Optimizing Responses (frmConcatenate.frm)
Private Sub Form_Load()
    Dim s1 As String
    Dim s2 As String
    Dim s3 As String
    Dim s4 As String
    Dim startTimer
    Dim diff
    Text1.Text = ""
    Me.Caption = "String Concatenation Comparison"
    Me.Show
    DoEvents
  
    Text1.Text = Text1.Text & "Start longConcat: "
    Text1.Refresh
    startTimer = Timer
    s1 = longConCat
    diff = Timer - startTimer
    Text1.Text = Text1.Text & diff & vbCrLf & vbCrLf
    Text1.Refresh
  
    Text1.Text = Text1.Text & "Start shortConcat: "
    Text1.Refresh
    startTimer = Timer
    s2 = shortConCat
    diff = Timer - startTimer
    Text1.Text = Text1.Text & diff & vbCrLf & vbCrLf
    Text1.Refresh
  
    Text1.Text = Text1.Text & "Start reallyShortConcat: "
    Text1.Refresh
    startTimer = Timer
    s3 = reallyShortConCat
    diff = Timer - startTimer
    Text1.Text = Text1.Text & diff & vbCrLf & vbCrLf
    Text1.Refresh
  
    Text1.Text = Text1.Text & "Start VariantConcat: "
    Text1.Refresh
    startTimer = Timer
    s1 = variantConcat()
    diff = Timer - startTimer
    Text1.Text = Text1.Text & diff & vbCrLf & vbCrLf
    Text1.Refresh
  
    Text1.Text = Text1.Text & "Start byteConcat: "
    Text1.Refresh
    startTimer = Timer
    s4 = byteConcat
    diff = Timer - startTimer
    Text1.Text = Text1.Text & diff & vbCrLf & vbCrLf
    Text1.Refresh
End Sub
Function longConCat() As String
    Dim s As String
    Dim i As Long
    For i = 1 To 10000
        s = s & "abcdefghijklmnopqrstuvwxyz"
    Next
    longConCat = s
End Function
Function shortConCat() As String
    Dim s As String
    Dim tmp As String
    Dim j As Long
    Dim i As Long
    For i = 1 To 1000
        tmp = ""
        For j = 1 To 10
            tmp = tmp & "abcdefghijklmnopqrstuvwxyz"
        Next
        s = s & tmp
    Next
    shortConCat = s
End Function
Function reallyShortConCat() As String
    Dim s As String
    Dim tmp As String
    Dim j As Long
    Dim i As Long
    For i = 1 To 100
        tmp = ""
        For j = 1 To 100
            tmp = tmp & "abcdefghijklmnopqrstuvwxyz"
        Next
        s = s & tmp
    Next
    reallyShortConCat = s
End Function
Function variantConcat() As String
    Dim s As String
    Dim V As Variant
    Dim tmp As Variant
    Dim j As Long
    Dim i As Long
    For i = 1 To 100
        tmp = ""
        For j = 1 To 100
            tmp = tmp & "abcdefghijklmnopqrstuvwxyz"
        Next
        V = V & tmp
    Next
    s = V
    variantConcat = s
End Function
Function byteConcat() As String
    Dim s As String
    Dim b() As Byte
    Dim i As Long
    Dim j As Long
    Dim ptr As Long
    Dim letters() As Byte
    letters = StrConv("abcdefghijklmnopqrstuvwxyz", vbFromUnicode)
    ReDim b(259999) As Byte
    ptr = 0
    For i = 1 To 10000
        For j = 0 To 25
            b(ptr) = letters(j)
            ptr = ptr + 1
        Next
    Next
    s = b()
    s = StrConv(s, vbUnicode)
    byteConcat = s
End Function
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.



Visual Basic Developer[ap]s Guide to ASP and IIS
Visual Basic Developer[ap]s Guide to ASP and IIS
ISBN: 782125573
EAN: N/A
Year: 2005
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net