Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
Dim lng As Long
Dim i As Long
Dim Start As Long
Dim Finish As Long
lng = 1234567890
Start = GetTickCount()
For i = 0 To 100000000
Call Deref1(VarPtr(lng))
Next
Finish = GetTickCount - Start
Text1.Text = Finish
Start = GetTickCount()
For i = 0 To 100000000
Call Deref2(VarPtr(lng))
Next
Finish = GetTickCount - Start
Text2.Text = Finish
End Sub
IN a module:
VB Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Sub GetMem4 Lib "msvbvm60" (ByVal Addr As Long, Value As Long)
Public Function Deref1(Addr As Long) As Long
CopyMemory ByVal VarPtr(Deref1), ByVal Addr, 4
End Function
Public Function Deref2(Addr As Long) As Long
GetMem4 Addr, Deref2
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
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
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
Public Declare Sub DerefLong Lib "msvbvm60" Alias "GetMem4" (Addr As Long, Value As Long)
and use this lot
VB Code:
Private Sub Command1_Click()
Dim lng As Long
Dim lng2 As Long
Dim i As Long
Dim Start As Long
Dim Finish As Long
lng = 1234567890
Start = GetTickCount()
For i = 0 To 100000000
CopyMemory lng2, lng, 4
Next
Finish = GetTickCount - Start
Text1.Text = Finish
lng2 = 0
Start = GetTickCount()
For i = 0 To 100000000
DerefLong lng, lng2
Next
Finish = GetTickCount - Start
Text2.Text = Finish
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
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
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?
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
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
"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
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