Staredit Network

Staredit Network -> Lite Discussion -> Code Optimization Suggestions
Report, edit, etc...Posted by Tarh on 2005-08-12 at 17:21:18
I know that Visual Basic has, and probably will always be, second to C++ when it comes to speed in bit calculations. Likewise, ASP (using VBScript) has, and probably will always be, second to PHP when it comes to speed in bit calculations. This functionalitiy, however, is needed for a project that I am working on. Since I am using ASP, optimization is also needed.

The basic idea was to create a function called CBit(), taking one ByVal parameter of type "byte" ("unsigned short int" in C++) (in theory - as VBScript uses all variants). The function would take that byte and output a string (char array) containing "1"s and "0"s in a series of bits. The value, of course, is returned by value.

I managed to whip up this little thing fairly quickly, but I'm sure that it could be made faster (don't get me wrong, it's very fast now - I just want to max it's speed).

CODE
Public Function CBit(number)
 Dim exponent, buffer
 exponent = 0
 Do While (2 ^ exponent) * 2 - 1 < number
  If exponent = 0 Then
   exponent = exponent + 3
  Else
   exponent = exponent + 4
  End If
 Loop
 Do
  If 2 ^ exponent <= number Then
   number = number - (2 ^ exponent)
   buffer = buffer + "1"
  Else
   buffer = buffer + "0"
  End If
  If exponent > 0 Then
   exponent = exponent - 1
  Else
   Exit Do
  End If
 Loop
 CBit = buffer
End Function


Any suggestions . . . at all . . . would be greatly appreciated!
Report, edit, etc...Posted by ihatett_da_hated on 2005-08-12 at 18:21:51
Is this .NET?
Report, edit, etc...Posted by Tarh on 2005-08-12 at 18:25:25
No
Report, edit, etc...Posted by ihatett_da_hated on 2005-08-12 at 18:43:37
Does vbscript have bitwise operators (in C++: x | y)?

If so, use them.
Report, edit, etc...Posted by Tarh on 2005-08-12 at 20:01:56
The following bitwise logical operators are supported in VBScript:

SyntaxxxxxxxxxxxxxxxxxxxxxxxxxxDescription
arg1 And arg2xxxxxxxxxxxxxxxxxxBitwise AND
arg1 Or arg2xxxxxxxxxxxxxxxxxxxBitwise OR
arg1 Xor arg2xxxxxxxxxxxxxxxxxxBitwise XOR
Not arg1xxxxxxxxxxxxxxxxxxxxxxxBitwise Not
expr1 Eqv expr2xxxxxxxxxxxxxxxxBitwise equivalence
expr1 Imp expr2xxxxxxxxxxxxxxxxBitwise implication


ADDITION:
Just for the record - here's the revised code (much faster).

VB 6.0:
CODE
Private Function CBit$(number&)
   Dim i&, buffer$
   i& = 0
   If number& = 0 Then _
       CBit$ = "0": Exit Function
   Do While (2& ^ i&) <= number&
       If ((2& ^ i&) Xor number&) < number& Then
           buffer$ = "1" & buffer$
       Else
           buffer$ = "0" & buffer$
       End If
       i& = i& + 1
   Loop
   CBit$ = buffer$
End Function


VBScript (All versions):
CODE
Function CBit(number)
   Dim i, buffer
   i = 0
   If number = 0 Then
       CBit = "0"
       Exit Function
   End If
   Do While (2 ^ i) <= number
       If ((2 ^ i) Xor number) < number Then
           buffer = "1" + buffer
       Else
           buffer = "0" + buffer
       End If
       i = i + 1
   Loop
   CBit = buffer
End Function
Next Page (1)