Results 1 to 11 of 11

Thread: Speed

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154

    Cool

    How can i speed up this Function?


    Code:
    Public Function InString(text As Variant, String1 As String) As Integer
    Dim y As Integer
    Dim x As Integer
    Dim i As Integer
    If String1 = "" Then
        InString = 0
        Exit Function
    End If
    For i = 1 To UBound(text)
    DoEvents
        x = InStr(1, text(i), String1)
        If x <> 0 Then
            y = y + 1
        End If
    Next i
    InString = y
    
    End Function


    Edited to add code tags

    [Edited by Bjwbell on 11-27-2000 at 11:23 PM]

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Well what slows down the process for you there is doevents, you have to put it somewhere where it fires more seldom, like using a double loop:
    Code:
    Public Function InString(text As Variant, String1 As String) As Integer
        Dim y%, i%, imax%
        If Len(String1) Then Exit Function
        For i = 1 To UBound(text)
            imax = i + 5000
            If imax > UBound(text) Then imax = UBound(text)
            For i = i To imax
               y = y - CBool(InStr(1, text(i), String1))
            Next i
            DoEvents
        Next i
        InString = y
    End Function
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    On a second trought, if you are using vb6 you could also pass text as a string array instead of variant, i'm not sure if thats much speed gain, but it's probably faster to access it than a variant all the time
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154
    How do you pass the text as a string array?

    [Edited by Bjwbell on 11-28-2000 at 12:27 AM]

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    I thought that would happen. Change i for j in the inner loop.
    Harry.

    "From one thing, know ten thousand things."

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Just to clarify, use this code:
    Code:
    Public Function InString(text As Variant, String1 As String) As Integer
        Dim y%, i%, imax%, j%
        If Len(String1) Then Exit Function
        For i = 1 To UBound(text)
            imax = i + 5000
            If imax > UBound(text) Then imax = UBound(text)
            For j = i To imax
               y = y - CBool(InStr(1, text(j), String1))
            Next i
            DoEvents
        Next i
        InString = y
    End Function
    Harry.

    "From one thing, know ten thousand things."

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154
    I did just did thats why i edited my post just now

    [Edited by Bjwbell on 11-28-2000 at 12:40 AM]

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Hmm pretty stupid of me, well it worked in java though..
    hehe "next j" there harry, you forgot that

    And heres how you declare the parameter a string array:
    Code:
    Public Function InString(text() as string, String1 As String) As Integer
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154
    Well here's what i came up with.

    Public Function InString(text() As String, String1 As String) As Integer
    Dim y%, x%, i%
    If String1 = "" Then
    InString = 0
    Exit Function
    End If
    For i = 1 To UBound(text)

    y = y - CBool(InStr(1, text(i), String1))

    Next i
    InString = y

    End Function



    [Edited by Bjwbell on 11-28-2000 at 01:18 AM]

  10. #10
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Oh yeah *D'oh*
    Harry.

    "From one thing, know ten thousand things."

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    am i missing something here? well uh i don't know whats going on if you keep editing your posts
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width