It gets slower and slower..... 
Author Message
 It gets slower and slower.....

I've written a procedure that processes an databound mshflexgrid control by
concatenating all the data into a string with a vbCrLf after each row is
appended. It also prints the time after every 100 rows are processed and as
you can see it gets slower and slower. WHY?

I'm using VB6 on Win98 on AMD K6 500 with 64 MB RAM

TIME                     ROWS
6/8/03 8:19:52 AM 0
6/8/03 8:19:52 AM 100
6/8/03 8:19:53 AM 200
6/8/03 8:19:55 AM 300
6/8/03 8:19:56 AM 400
6/8/03 8:19:59 AM 500
6/8/03 8:20:02 AM 600
6/8/03 8:20:06 AM 700
6/8/03 8:20:11 AM 800
6/8/03 8:20:16 AM 900
6/8/03 8:20:22 AM 1000
6/8/03 8:20:28 AM 1100
6/8/03 8:20:36 AM 1200
6/8/03 8:20:44 AM 1300

Public Sub ProcessGrid(grid As MSHFlexGrid)

    Dim r As Integer, c As Integer
    Dim sBigString As String

    With grid

    For r = 0 To .Rows - 1

   ' The following line prints the time after each 100 rows

     If r Mod 100 = 0 Then Debug.Print Now & " " & r

        For c = 0 To .Cols - 1

             sBigString = sBigString & .TextMatrix(r, c)  & " "

       Next c

        sBigString = sBigString & vbCrLf

    Next

   End With

End Sub



Thu, 24 Nov 2005 11:31:07 GMT  
 It gets slower and slower.....
Because your sBigString was longer and longer.

Quote:
> I've written a procedure that processes an databound mshflexgrid control
by
> concatenating all the data into a string with a vbCrLf after each row is
> appended. It also prints the time after every 100 rows are processed and
as
> you can see it gets slower and slower. WHY?

> I'm using VB6 on Win98 on AMD K6 500 with 64 MB RAM

> TIME                     ROWS
> 6/8/03 8:19:52 AM 0
> 6/8/03 8:19:52 AM 100
> 6/8/03 8:19:53 AM 200
> 6/8/03 8:19:55 AM 300
> 6/8/03 8:19:56 AM 400
> 6/8/03 8:19:59 AM 500
> 6/8/03 8:20:02 AM 600
> 6/8/03 8:20:06 AM 700
> 6/8/03 8:20:11 AM 800
> 6/8/03 8:20:16 AM 900
> 6/8/03 8:20:22 AM 1000
> 6/8/03 8:20:28 AM 1100
> 6/8/03 8:20:36 AM 1200
> 6/8/03 8:20:44 AM 1300

> Public Sub ProcessGrid(grid As MSHFlexGrid)

>     Dim r As Integer, c As Integer
>     Dim sBigString As String

>     With grid

>     For r = 0 To .Rows - 1

>    ' The following line prints the time after each 100 rows

>      If r Mod 100 = 0 Then Debug.Print Now & " " & r

>         For c = 0 To .Cols - 1

>              sBigString = sBigString & .TextMatrix(r, c)  & " "

>        Next c

>         sBigString = sBigString & vbCrLf

>     Next

>    End With

> End Sub



Thu, 24 Nov 2005 11:59:54 GMT  
 It gets slower and slower.....

Quote:
> I've written a procedure that processes an databound mshflexgrid control by
> concatenating all the data into a string with a vbCrLf after each row is
> appended. It also prints the time after every 100 rows are processed and as
> you can see it gets slower and slower. WHY?

Because each time you add more to the string, VB has to go find a suitable
place in memory big enough to hold the old string plus the new data, and
then has to copy the old string into place before adding the new data.
That old string keeps getting bigger, and bigger, requiring more and more time
to move....

Here is a faster routine I have broken into two subs to better see what is
going on, you might want to put it all in one routine, or you could just
add the AddToString routine and use that.  Note that at the start of the
loops z is set to 1, that is required to avoid an error in the AddToString
sub.  (Rather than check Pos there every time, even though only the first
use needs to be checked...)

Time that and report your results....

HTH
LFS

Private Sub Form_Load()
Faster
End Sub

Private Sub Faster()
Dim result As String, newstuff As String
Dim x As Long, y As Long, z As Long

  newstuff = "ABC"
  z = 1
  For x = 0 To 3
    For y = 0 To 3
      ' Add new stuff
      AddToString result, newstuff, z
    Next y

    ' Add line feed
    AddToString result, vbCrLf, z
  Next x

  ' Trim off excess spaces
  result = Trim$(result)

Debug.Print result
End Sub

Private Sub AddToString(ByRef Dst As String, ByRef Src As String, ByRef Pos As Long)
    ' make room less often
    If Pos + Len(Src) >= Len(Dst) Then
      Dst = Dst + Space$(8000 + Len(Src))
    End If

    ' copy into place manually
    Mid(Dst, Pos, Len(Src)) = Src
    Pos = Pos + Len(Src) + 1
End Sub



Thu, 24 Nov 2005 12:27:19 GMT  
 It gets slower and slower.....
Thanks.


Quote:

> > I've written a procedure that processes an databound mshflexgrid control
by
> > concatenating all the data into a string with a vbCrLf after each row is
> > appended. It also prints the time after every 100 rows are processed and
as
> > you can see it gets slower and slower. WHY?

> Because each time you add more to the string, VB has to go find a suitable
> place in memory big enough to hold the old string plus the new data, and
> then has to copy the old string into place before adding the new data.
> That old string keeps getting bigger, and bigger, requiring more and more
time
> to move....

> Here is a faster routine I have broken into two subs to better see what is
> going on, you might want to put it all in one routine, or you could just
> add the AddToString routine and use that.  Note that at the start of the
> loops z is set to 1, that is required to avoid an error in the AddToString
> sub.  (Rather than check Pos there every time, even though only the first
> use needs to be checked...)

> Time that and report your results....

> HTH
> LFS

> Private Sub Form_Load()
> Faster
> End Sub

> Private Sub Faster()
> Dim result As String, newstuff As String
> Dim x As Long, y As Long, z As Long

>   newstuff = "ABC"
>   z = 1
>   For x = 0 To 3
>     For y = 0 To 3
>       ' Add new stuff
>       AddToString result, newstuff, z
>     Next y

>     ' Add line feed
>     AddToString result, vbCrLf, z
>   Next x

>   ' Trim off excess spaces
>   result = Trim$(result)

> Debug.Print result
> End Sub

> Private Sub AddToString(ByRef Dst As String, ByRef Src As String, ByRef
Pos As Long)
>     ' make room less often
>     If Pos + Len(Src) >= Len(Dst) Then
>       Dst = Dst + Space$(8000 + Len(Src))
>     End If

>     ' copy into place manually
>     Mid(Dst, Pos, Len(Src)) = Src
>     Pos = Pos + Len(Src) + 1
> End Sub



Fri, 25 Nov 2005 10:28:03 GMT  
 It gets slower and slower.....
Try using split and join functions

split takes a string y = "xxxx,yyyy,zzzz" and creates an array into a variant

e.g.
x = split(y,",")

join takes an array and creates a string

y = join(x,vbCRLF)

super quick

Regards
Wookie

Quote:

> I've written a procedure that processes an databound mshflexgrid control by
> concatenating all the data into a string with a vbCrLf after each row is
> appended. It also prints the time after every 100 rows are processed and as
> you can see it gets slower and slower. WHY?

> I'm using VB6 on Win98 on AMD K6 500 with 64 MB RAM

> TIME                     ROWS
> 6/8/03 8:19:52 AM 0
> 6/8/03 8:19:52 AM 100
> 6/8/03 8:19:53 AM 200
> 6/8/03 8:19:55 AM 300
> 6/8/03 8:19:56 AM 400
> 6/8/03 8:19:59 AM 500
> 6/8/03 8:20:02 AM 600
> 6/8/03 8:20:06 AM 700
> 6/8/03 8:20:11 AM 800
> 6/8/03 8:20:16 AM 900
> 6/8/03 8:20:22 AM 1000
> 6/8/03 8:20:28 AM 1100
> 6/8/03 8:20:36 AM 1200
> 6/8/03 8:20:44 AM 1300

> Public Sub ProcessGrid(grid As MSHFlexGrid)

>     Dim r As Integer, c As Integer
>     Dim sBigString As String

>     With grid

>     For r = 0 To .Rows - 1

>    ' The following line prints the time after each 100 rows

>      If r Mod 100 = 0 Then Debug.Print Now & " " & r

>         For c = 0 To .Cols - 1

>              sBigString = sBigString & .TextMatrix(r, c)  & " "

>        Next c

>         sBigString = sBigString & vbCrLf

>     Next

>    End With

> End Sub



Fri, 25 Nov 2005 22:08:48 GMT  
 It gets slower and slower.....

Quote:
>Try using split and join functions

Funny that

I've always found that : Spliff and Joint  slows things down

A bit like the VB6 Split() and Join()

Quote:

>split takes a string y = "xxxx,yyyy,zzzz" and creates an array into a variant

>e.g.
>x = split(y,",")

>join takes an array and creates a string

>y = join(x,vbCRLF)

>super quick



Fri, 25 Nov 2005 22:39:41 GMT  
 It gets slower and slower.....
It's because of the repeated memory reallocation for the dynamic string.
Here's an article with sample code for a string class that is much
faster. -- Scott

http://www.informit.com/content/index.asp?session_id={A4BDACFA-2F82-4AC9-A009-77C746E10891}&product_id={51FF8561-0B26-49DB-9AE2-1C4716CCBFEE}


Quote:
> I've written a procedure that processes an databound mshflexgrid control
by
> concatenating all the data into a string with a vbCrLf after each row is
> appended. It also prints the time after every 100 rows are processed and
as
> you can see it gets slower and slower. WHY?

> I'm using VB6 on Win98 on AMD K6 500 with 64 MB RAM

> TIME                     ROWS
> 6/8/03 8:19:52 AM 0
> 6/8/03 8:19:52 AM 100
> 6/8/03 8:19:53 AM 200
> 6/8/03 8:19:55 AM 300
> 6/8/03 8:19:56 AM 400
> 6/8/03 8:19:59 AM 500
> 6/8/03 8:20:02 AM 600
> 6/8/03 8:20:06 AM 700
> 6/8/03 8:20:11 AM 800
> 6/8/03 8:20:16 AM 900
> 6/8/03 8:20:22 AM 1000
> 6/8/03 8:20:28 AM 1100
> 6/8/03 8:20:36 AM 1200
> 6/8/03 8:20:44 AM 1300

> Public Sub ProcessGrid(grid As MSHFlexGrid)

>     Dim r As Integer, c As Integer
>     Dim sBigString As String

>     With grid

>     For r = 0 To .Rows - 1

>    ' The following line prints the time after each 100 rows

>      If r Mod 100 = 0 Then Debug.Print Now & " " & r

>         For c = 0 To .Cols - 1

>              sBigString = sBigString & .TextMatrix(r, c)  & " "

>        Next c

>         sBigString = sBigString & vbCrLf

>     Next

>    End With

> End Sub



Mon, 28 Nov 2005 04:46:16 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Slow, slow, slow search on VB6, ADO and SS7

2. VB4 is slow..slow....slow

3. Insert into SQL Server slow, slow, slow...

4. SLOW - ADO ASP FOXPRO CDX - SLOW

5. Runtime SQL gets slower with each addl. criteria

6. ODBC Getting Slow

7. Access DB getting slow

8. This routine runs slower slower each time it's used. Any Ideas why?

9. Slow form_load due to slow MSADORA connection: how does one deal with this?

10. FindFirst is slow

11. Refreshing really slow...

12. slow on win2000

 

 
Powered by phpBB® Forum Software