VBspeed / Bits / MakeWord
VBspeed © 2000-10, updated: 06-Dec-2000
MakeWord


Function MakeWord
Combines two bytes to one 2-byte Word (aka Integer).
Code
MakeWord01
Public Function MakeWord01(LoByte As Byte, HiByte As Byte) As Integer
  If HiByte And &H80 Then
    MakeWord01 = ((HiByte * &H100&) + LoByte) Or &HFFFF0000
  Else
    MakeWord01 = (HiByte * &H100) + LoByte
  End If
End Function
  
MakeWord02
Public Function MakeWord02(LoByte As Byte, HiByte As Byte) As Integer
  If HiByte And &H80 Then
    MakeWord02 = ((HiByte * &H100&) Or LoByte) Or &HFFFF0000
  Else
    MakeWord02 = (HiByte * &H100) Or LoByte
  End If
End Function
  
MakeWord03
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Public Function MakeWord03(LoByte As Byte, HiByte As Byte) As Integer CopyMemory MakeWord03, LoByte, 1& CopyMemory ByVal VarPtr(MakeWord03) + 1&, HiByte, 1& End Function
MakeWord04
Private Type T1Int
  iWord As Integer
End Type
Private Type T2Byte
  bLoByte As Byte
  bHiByte As Byte
End Type

Public Function MakeWord04(LoByte As Byte, HiByte As Byte) As Integer Dim u2Byte As T2Byte Dim u1Int As T1Int u2Byte.bLoByte = LoByte u2Byte.bHiByte = HiByte LSet u1Int = u2Byte MakeWord04 = u1Int.iWord End Function
Calls
1iRet = MakeWord(1, 1)
2iRet = MakeWord(255, 255)
Charts
 VB5 Charts
Code
MakeWord01 
MakeWord02 
MakeWord03 
MakeWord04 
Call 1
11.000.060Ás
21.050.064Ás
410.090.610Ás
33.090.187Ás
Call 2
11.000.060Ás
21.050.063Ás
410.090.610Ás
33.100.187Ás
 VB6 Charts
Code
MakeWord01 
MakeWord02 
MakeWord03 
MakeWord04 
Call 1
21.230.078Ás
11.000.064Ás
49.610.613Ás
32.890.184Ás
Call 2
21.200.076Ás
11.000.064Ás
49.610.613Ás
32.890.184Ás
Conclusions
  • Hm, the situation is not as clear as in the MakeDWord competition: it's either MakeWord01 or MakeWord02 depending on which VB version you use.
    Note, that the only difference between MakeWord01 and MakeWord02 is the use of "Or" instead of "+" in the latter, which gives it a considerable boost under VB6.
  • What the charts do not reveal: under VB6 the timing results of MakeWord01 and MakeWord02 exhibit an unusal amount of scatter: about 20%, vs only 3% under VB5. No idea what's going on there.
  • The API-doping of MakeWord03 proves to be high-class Valium.
  • Using UDTs and LSet (MakeWord04) is an idea with no future.
The bottom line is:
  1. Use VB5 and MakeWord01. If you're one of the unlucky VB6-users then stick to MakeWord02.
Got comments? How to read all those numbers

top




VBspeed © 2000-10 by Donald Lessau