Results 1 to 12 of 12

Thread: Fast(er) Dereference in VB6

  1. #1

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Fast(er) Dereference in VB6

    In a form:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetTickCount Lib "kernel32" () As Long
    4.  
    5. Private Sub Command1_Click()
    6.  
    7.     Dim lng As Long
    8.     Dim i As Long
    9.     Dim Start As Long
    10.     Dim Finish As Long
    11.    
    12.     lng = 1234567890
    13.    
    14.     Start = GetTickCount()
    15.     For i = 0 To 100000000
    16.         Call Deref1(VarPtr(lng))
    17.     Next
    18.     Finish = GetTickCount - Start
    19.     Text1.Text = Finish
    20.    
    21.     Start = GetTickCount()
    22.     For i = 0 To 100000000
    23.         Call Deref2(VarPtr(lng))
    24.     Next
    25.     Finish = GetTickCount - Start
    26.     Text2.Text = Finish
    27.    
    28. End Sub

    IN a module:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    4. Private Declare Sub GetMem4 Lib "msvbvm60" (ByVal Addr As Long, Value As Long)
    5.  
    6.  
    7. Public Function Deref1(Addr As Long) As Long
    8.     CopyMemory ByVal VarPtr(Deref1), ByVal Addr, 4
    9. End Function
    10.  
    11. Public Function Deref2(Addr As Long) As Long
    12.     GetMem4 Addr, Deref2
    13. End Function

    Compiled with all optimisations on I get:

    Deref1:6860
    Deref2:3484
    "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. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Fast(er) Dereference in VB6

    Hey, try the PutMem's!

    Nice.

    I've been planning a Codebank thread on the fastest ways to use pointers in VB. This just made it in

  3. #3

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Fast(er) Dereference in VB6

    Looking at returning the value directly onto a long variable by pretending - using voids and what not - that VB is calling a function, when really it's just running some assembly.

    That way it can go in-line, and we can dump all that expensive HRESULT stuff (although I can get it to do similar by declaring the GetMem in a typelib)

    Getting a bit stuck on the VB function stack frame, though. Intels 'wonderful' byte ordering is err 'wonderful' ?
    "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. #4

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Fast(er) Dereference in VB6

    Penegate:

    I followed your link above. I have to admit that I'd never thought of using driver level API's but there's a load there that will make some of my code absolutely fly.

    Nice one mate. Much appreciated, indeed

    "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

  5. #5

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Fast(er) Dereference in VB6

    If you re-Declare as

    VB Code:
    1. Public Declare Sub DerefLong Lib "msvbvm60" Alias "GetMem4" (Addr As Long, Value As Long)

    and use this lot

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Dim lng As Long
    4.     Dim lng2 As Long
    5.     Dim i As Long
    6.     Dim Start As Long
    7.     Dim Finish As Long
    8.    
    9.     lng = 1234567890
    10.    
    11.     Start = GetTickCount()
    12.     For i = 0 To 100000000
    13.         CopyMemory lng2, lng, 4
    14.     Next
    15.     Finish = GetTickCount - Start
    16.     Text1.Text = Finish
    17.    
    18.     lng2 = 0
    19.    
    20.     Start = GetTickCount()
    21.     For i = 0 To 100000000
    22.         DerefLong lng, lng2
    23.     Next
    24.     Finish = GetTickCount - Start
    25.     Text2.Text = Finish
    26.    
    27. End Sub

    Then the performance is:
    CopyMemory: 4906
    DerefLong:: 1781

    Now that can't be bad for 100million iterations, can it

    The lesson here, methinks, is that calls to VB functions severely inhibit the speed of this function.

    (BTW - this is a different computer - I'm at home, now)
    "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

  6. #6
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Fast(er) Dereference in VB6

    whats the purpose of dereference?

  7. #7

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Fast(er) Dereference in VB6

    A lot of data in a computer program is stored as a pointer to it's memory address.

    Dereferencing means convert the address to the value it points to.

    Something that you can do in almost every language apart from VB!
    "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

  8. #8

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Fast(er) Dereference in VB6

    Here's an example project extensively using this form of dereferencing.

    It is an implementation of some basic matrix handling routines. I have no idea how the speed compares to implementing matrices using a safearray.

    What do you think?
    Attached Files Attached Files
    Last edited by yrwyddfa; Sep 27th, 2005 at 11:33 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

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

    Re: Fast(er) Dereference in VB6

    What's it supposed to output? I get 70.

  10. #10

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Fast(er) Dereference in VB6

    Yup: it's the matrix multiplication of
    (1 2 3 4)

    (5
    6
    7
    8)

    which returns a 1x1 matrix containing the element 70.
    "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

  11. #11

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Fast(er) Dereference in VB6

    If you want a more complex example you can try this:

    VB Code:
    1. Sub Main()
    2.  
    3.     On Error GoTo ERR_Main
    4.    
    5.     Dim pM1 As Long
    6.     Dim pM2 As Long
    7.     Dim pM3 As Long
    8.     Dim i As Long
    9.     Dim j As Long
    10.    
    11.     pM1 = MCreate(2, 4)
    12.     pM2 = MCreate(4, 3)
    13.    
    14.     PutElement pM1, 1, 1, 1
    15.     PutElement pM1, 1, 2, 2
    16.     PutElement pM1, 1, 3, 3
    17.     PutElement pM1, 1, 4, 4
    18.     PutElement pM1, 2, 1, 5
    19.     PutElement pM1, 2, 2, 6
    20.     PutElement pM1, 2, 3, 7
    21.     PutElement pM1, 2, 4, 8
    22.    
    23.     PutElement pM2, 1, 1, 1
    24.     PutElement pM2, 1, 2, 2
    25.     PutElement pM2, 1, 3, 3
    26.     PutElement pM2, 2, 1, 4
    27.     PutElement pM2, 2, 2, 5
    28.     PutElement pM2, 2, 3, 6
    29.     PutElement pM2, 3, 1, 7
    30.     PutElement pM2, 3, 2, 8
    31.     PutElement pM2, 3, 3, 9
    32.     PutElement pM2, 4, 1, 10
    33.     PutElement pM2, 4, 2, 11
    34.     PutElement pM2, 4, 3, 12
    35.    
    36.     pM3 = MMultiply(pM1, pM2)
    37.    
    38.     For i = 1 To MRows(pM3)
    39.         For j = 1 To MColumns(pM3)
    40.             Debug.Print GetElement(pM3, i, j), ;
    41.         Next
    42.         Debug.Print
    43.     Next
    44.  
    45.     Exit Sub
    46.    
    47. ERR_Main:
    48.     Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
    49. End Sub

    The answer should be

    (70 80 90
    158 184 210)
    "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. #12

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Fast(er) Dereference in VB6

    Any thoughts on this matrix thing on getting it to be more efficient?
    "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

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