Page 2 of 2 FirstFirst 12
Results 41 to 60 of 60

Thread: argh! count number of full stops??

  1. #41
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: argh! count number of full stops??

    VB Code:
    1. Public Function Sisic4(pStr As Long, pFind As Long, lenStr As Long, lenFind As Long) As Long
    2.  
    3.     Dim i As Long
    4.     Dim j As Long
    5.     Dim Flag As Long
    6.    
    7.     CopyMemory BufStr(0), ByVal pStr, lenStr
    8.     CopyMemory BufFind(0), ByVal pFind, lenFind
    9.    
    10.     i = lenStr - 1
    11.    
    12.     If lenFind = 2 Then
    13.         Do Until i < lenFind
    14.             If BufStr(i - 1) = BufFind(0) Then
    15.                 Sisic4 = Sisic4 + 1
    16.             End If
    17.             i = i - 2
    18.         Loop
    19.     Else
    20.         Do Until i < lenFind
    21.             Flag = 0
    22.             j = lenFind - 1
    23.             Do Until j < 0
    24.                 If Not (BufStr(i - (lenFind - j)) = BufFind(j - 1)) Then
    25.                     Flag = -1
    26.                     Exit Do
    27.                 End If
    28.                 j = j - 2
    29.             Loop
    30.             If Flag = 0 Then
    31.                 Sisic4 = Sisic4 + 1
    32.             End If
    33.             i = i - 2
    34.         Loop
    35.     End If
    36.    
    37. End Function

    Removing the for/loops makes it go faster!!
    Sisic3=469, Sisic4=438
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  2. #42
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: argh! count number of full stops??

    It's more efficient to pass Long values ByVal instead of ByRef because ByRef results in an indirection whereas ByVal doesn't. Seeing as a pointer is the same size as a Long anyway, you lose instead of gaining. I deleted the benchmarking code so see if that gives any increase in times.

  3. #43
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: argh! count number of full stops??

    No! It makes it slower

    Sisic3=475
    Sisic4=544
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  4. #44
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: argh! count number of full stops??

    Did you put the code in the form. I just realised it has to do a vtable lookup every time it calls the function, so I put it in a module and it is much faster... in fact your Sisic3 function is faster than Sisic4 that you posted in #41.

  5. #45
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: argh! count number of full stops??

    ByVal is faster... told you

    InStrCount3: 375 ms
    Sisic3: 344 ms
    Sisic4 with ByRef: 343 ms
    Sisic4 with ByVal: 313 ms

  6. #46
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: argh! count number of full stops??

    Yeah I put the function in the form
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  7. #47
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: argh! count number of full stops??

    I managed to get mine to the same speed as yours, they are now basically exactly the same code

    It is indeed faster to pass the string length as a parameter, and to use module-level buffers instead of statics.

    So far I can't really think of any other optimisations to do.

  8. #48
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: argh! count number of full stops??

    I was surprised by the minor increase in speed from dumping the for/loops.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  9. #49
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: argh! count number of full stops??

    Quote Originally Posted by penagate
    I managed to get mine to the same speed as yours, they are now basically exactly the same code

    It is indeed faster to pass the string length as a parameter, and to use module-level buffers instead of statics.

    So far I can't really think of any other optimisations to do.
    I quite often allocate myself 2k of heap at app startup, and then just use that as a workspace. The main problem is, of course, dereferencing. CopyMemory is fine, but for dereferencing it's pretty slow .. .
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  10. #50
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: argh! count number of full stops??

    I toyed with the idea of using HeapAlloc, but then the thought of calling CopyMemory on every loop iteration through the string just put me right off.

  11. #51
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: argh! count number of full stops??

    I tried it and it was slower.

    Any ideas why for/loops are slower than do/loops. I have been under the impression that for/loops were faster.

    Can't remember where I got that idea, from!
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  12. #52
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: argh! count number of full stops??

    Neither can I, but I dunno why yours got faster. I tried it too and it over several runs it made no (noticable) difference either way.

    BTW, I take that back about not thinking of any other optimisations to do
    VB Code:
    1. Function InStrCount4( _
    2.     ByVal pszString As Long, _
    3.     ByVal pszFind As Long, _
    4.     ByVal pchSearchChar As Byte, _
    5.     ByVal lStringLen As Long, _
    6.     ByVal lFindLen As Long _
    7. ) As Long
    8. Dim i               As Long
    9. Dim j               As Long
    10.  
    11.     CopyMemory mchBuf(0), ByVal pszString, lStringLen
    12.  
    13.     If (lFindLen = 2) Then
    14.         If (pchSearchChar = 0) Then _
    15.             CopyMemory pchSearchChar, ByVal pszFind, 1
    16.         Do
    17.             If (mchBuf(i) = pchSearchChar) Then _
    18.                 InStrCount4 = InStrCount4 + 1
    19.             i = i + 2
    20.         Loop Until i = lStringLen
    21.  
    22.       Else
    23.         CopyMemory mchFind(0), ByVal pszFind, lFindLen
    24.         Do
    25.             Do
    26.                 If (mchBuf(i + j) <> mchFind(j)) Then
    27.                     Exit Do
    28.                   Else
    29.                     If (j = lFindLen) Then _
    30.                         InStrCount4 = InStrCount4 + 1
    31.                 End If
    32.                 j = j + 2
    33.             Loop Until j = lFindLen
    34.             i = i + lFindLen
    35.         Loop Until i = lStringLen
    36.     End If
    37. End Function

    Results:
    Sisic3: 328 ms
    Sisc4 (ByVal): 312 ms
    InStrCount4: 282 ms


  13. #53
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: argh! count number of full stops??

    Yeah that's pretty fast. Haven't had time to look at it properly (and probably won't) What have you done? (I haven't got your intermediate code!)
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  14. #54
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: argh! count number of full stops??

    Got rid of Statics, passed string lengths as parameters, that made it as fast as yours.

    Added a choice between passing a pointer to find string buffer, and passing a byte character directly, that made it faster than yours.

  15. #55
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: argh! count number of full stops??

    Oh yeah. That's pretty cool.

    If the CopyMemory is typelib defined - then that will make it faster, too (I believe - I haven't tried it yet - but might do some point this week)
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  16. #56
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: argh! count number of full stops??

    according to MSDN Help using ordinal numbers is supposed to be faster than function names. However, I tried, and it made no difference whatsoever. You'd think it would be handled the same way by the linker anyway, so I fail to see the logic behind that one.

  17. #57
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: argh! count number of full stops??

    VB Code:
    1. [
    2.     uuid(842b47f0-0b27-11da-8cd6-0800200c9a66),
    3.     version(0.1),
    4.     helpstring("Win32 API Kernel32.DLL")
    5. ]
    6. library Win32Stuff
    7. {
    8.     importlib("stdole2.tlb");
    9.  
    10.     [dllname("kernel32")]
    11.  
    12.     module kernel32
    13.     {
    14.         [
    15.             entry("RtlMoveMemory")
    16.         ]
    17.         VOID CopyMemory ([in,out] LONG* lpDestination, [in,out] LONG* lpSource, [in] LONG Length);
    18.     }
    19. }

    Compile this using MkTypLib (you may need to set a few path environment variables) remove 'CopyMemory' from your declare list and reference the compiled type library.

    I get around 18% performance boost.

    VB Code:
    1. Public Function Sisic4(ByVal pStr As Long, ByVal pFind As Long, ByVal lenStr As Long, ByVal lenFind As Long) As Long
    2.  
    3.     Dim i As Long
    4.     Dim j As Long
    5.     Dim Flag As Long
    6.    
    7.     CopyMemory VarPtr(BufStr(0)), ByVal pStr, ByVal lenStr
    8.     CopyMemory VarPtr(BufFind(0)), ByVal pFind, ByVal lenFind
    9.    
    10.     i = lenStr - 1
    11.    
    12.     If lenFind = 2 Then
    13.         Do Until i < lenFind
    14.             If BufStr(i - 1) = BufFind(0) Then
    15.                 Sisic4 = Sisic4 + 1
    16.             End If
    17.             i = i - 2
    18.         Loop
    19.     Else
    20.         Do Until i < lenFind
    21.             Flag = 0
    22.             j = lenFind - 1
    23.             Do Until j < 0
    24.                 If Not (BufStr(i - (lenFind - j)) = BufFind(j - 1)) Then
    25.                     Flag = -1
    26.                     Exit Do
    27.                 End If
    28.                 j = j - 2
    29.             Loop
    30.             If Flag = 0 Then
    31.                 Sisic4 = Sisic4 + 1
    32.             End If
    33.             i = i - 2
    34.         Loop
    35.     End If
    36.    
    37. End Function
    .. note the varptr function is used to get the ptr to the first array entry.
    Last edited by yrwyddfa; Sep 5th, 2005 at 04:46 AM.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  18. #58
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: argh! count number of full stops??

    Compiled the tlb, but I get a Type Mismatch on the VarPtr parameter?

  19. #59
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: argh! count number of full stops??

    Weird!

    Have you commented out the existing CopyMemory declare?
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  20. #60
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: argh! count number of full stops??

    Yup.

    It likes it when I remove all the ByVal keywords, but then when I run my function, it bombs out with a subscript out of range error. Further inspection showed that my i variable had been overwritten with gibberish

Page 2 of 2 FirstFirst 12

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