VBspeed / Bits / MakeDWord
VBspeed © 2000-10, updated: 30-Dec-2000
MakeDWord


Function MakeDWord
Combines two 2-byte Words (aka Integers) to one 4-byte DoubleWord (aka Long).
Code
MakeDWord01
Public Function MakeDWord01(LoWord As Integer, HiWord As Integer) As Long
' by Donald, donald@xbeat.net, 20001205
  MakeDWord01 = (HiWord * &H10000) + (LoWord And &HFFFF&)
End Function
  
MakeDWord02
Public Function MakeDWord02(LoWord As Integer, HiWord As Integer) As Long
' by Donald, donald@xbeat.net, 20001205
  MakeDWord02 = (HiWord * &H10000) Or (LoWord And &HFFFF&)
End Function
  
MakeDWord03
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Public Function MakeDWord03(LoWord As Integer, HiWord As Integer) As Long ' by Donald, donald@xbeat.net, 20001205 CopyMemory MakeDWord03, LoWord, 2& CopyMemory ByVal VarPtr(MakeDWord03) + 2&, HiWord, 2& End Function
MakeDWord04
Private Type T1Long
  lDWord As Long
End Type
Private Type T2Int
  iLoWord As Integer
  iHiWord As Integer
End Type

Public Function MakeDWord04(LoWord As Integer, HiWord As Integer) As Long ' by Donald, donald@xbeat.net, 20001205 Dim u2Int As T2Int Dim u1Long As T1Long u2Int.iLoWord = LoWord u2Int.iHiWord = HiWord LSet u1Long = u2Int MakeDWord04 = u1Long.lDWord End Function
MakeDWord05
Public Function MakeDWord05(ByVal LoWord As Integer, ByVal HiWord As Integer) As Long
' by G.Beckmann, G.Beckmann@NikoCity.de, 20001207
  MakeDWord05 = (HiWord * &H10000) Or (LoWord And &HFFFF&)
End Function
  
MakeDWord06
Public Function MakeDWord06(ByVal LoWord As Integer, ByVal HiWord As Integer) As Long
' by Karl E. Peterson, http://www.mvps.org/vb, 20001207
  ' High word is coerced to Long to allow it to
  ' overflow limits of multiplication which shifts
  ' it left.
  MakeDWord06 = (CLng(HiWord) * &H10000) Or (LoWord And &HFFFF&)
End Function
  
Calls
1lRet = MakeDWord(1, 1)
2lRet = MakeDWord(-1, -1)
Charts
 VB5 Charts
Code
MakeDWord01 
MakeDWord02 
MakeDWord03 
MakeDWord04 
MakeDWord05 
MakeDWord06 
Call 1
41.5043.130ns
31.0730.697ns
620.94603.420ns
56.45185.929ns
11.0028.815ns
21.0229.368ns
Call 2
41.5043.191ns
31.0730.657ns
620.97603.455ns
56.46185.937ns
11.0028.772ns
21.0229.383ns
 VB6 Charts
Code
MakeDWord01 
MakeDWord02 
MakeDWord03 
MakeDWord04 
MakeDWord05 
MakeDWord06 
Call 1
41.5343.229ns
31.0830.580ns
621.18597.868ns
56.56185.257ns
21.0228.739ns
11.0028.230ns
Call 2
41.5343.207ns
31.0930.629ns
621.20597.676ns
56.57185.353ns
21.0228.736ns
11.0028.197ns
Conclusions
  • Note, that the only difference between MakeDWord01 and MakeDWord02 is the use of "Or" instead of "+" in the latter, which gives it a considerable boost.
  • Passing the arguments ByVal (MakeDWord05/06) looks better than ByRef (MakeDWord02).
    Note, however, that this is only true if the passed variables or constants have procedure scope; when you pass global scope arguments, ByRef is better.
  • Regarding the differences between MakeDWord05 and MakeDWord06: do not take those numbers too serious. Functions that take about 30 nanoseconds per call are real fast, actually so fast that normally hidden conditions and processes become visible in the timing results. In other words, our measurements are distorted by a type of noise that is inherent to the execution of computer programs (and *not* just the normal background-hum of a multitasking OS), and probably (even more tragic!) also inherent to the measurement of execution speed itself. There is a way out of this dilemma, but it's not VB anymore:
    At these speed levels, performance measurement is best (if not only) done by disassembling the executable and counting assembly instructions.
  • As could be expected, there are no significant differences between the two calls. A byte is a byte.
  • The API-doping of MakeDWord03 proves to be high-class Valium.
  • Using UDTs and LSet (MakeDWord04) is an idea with no future.
Got comments? How to read all those numbers

top




VBspeed © 2000-10 by Donald Lessau