Skip to content

Instantly share code, notes, and snippets.

@AdamSpeight2008
Created November 23, 2015 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamSpeight2008/c7d4469a45542a6e5414 to your computer and use it in GitHub Desktop.
Save AdamSpeight2008/c7d4469a45542a6e5414 to your computer and use it in GitHub Desktop.
Digit Character Value (Test Code)
Imports System.Runtime.CompilerServices
Module Module1
Dim sw As New Stopwatch
Dim taken As Long = 0
Sub Main()
Dim methods() As Func(Of Char, Byte) = {AddressOf XX.Method1,
AddressOf XX.Method2,
AddressOf XX.IntegralLiteralCharacterValue,
AddressOf XX.Method3,
AddressOf XX.Method4,
AddressOf XX.Method5,
AddressOf XX.Method6}
For xi = 0 To methods.Length - 1
taken = 0
sw.Reset()
For Each ch In "0123456789ABCDEFabcdef0123456789ABCDEFabcdef"
Console.Write("Method_{0}({1}) : ", xi + 1, ch)
Test(ch, methods(xi))
taken += sw.ElapsedMilliseconds
Next
Console.WriteLine("{0}ms (Avg:= {1}ms)", taken, taken \ 42)
Console.WriteLine()
Console.WriteLine()
Next
End Sub
Sub Test(ch As Char, method As Func(Of Char, Byte))
sw.Restart()
Dim sum As Integer = 0
For i As Integer = 1 To 10000000
sum += method(ch)
Next
sw.Stop()
Console.WriteLine("{0} {1}", sum, sw.ElapsedMilliseconds)
End Sub
End Module
Public Class XX
Private Shared _Keys As Char() = {"0"c, "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, "8"c, "9"c,
"A"c, "B"c, "C"c, "D"c, "E"c, "F"c, "a"c, "b"c, "c"c, "d"c, "e"c, "f"c, ' Fullwidth versions are next.
"0"c, "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, "8"c, "9"c,
"A"c, "B"c, "C"c, "D"c, "E"c, "F"c, "a"c, "b"c, "c"c, "d"c, "e"c, "f"c}
Private Shared _Values As Byte() = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 10, 11, 12, 13, 14, 15,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 10, 11, 12, 13, 14, 15}
Private Shared _CharacterValues_ As New Dictionary(Of Char, Byte)(capacity:=44) _
From {{"0"c, 0}, {"1"c, 1}, {"2"c, 2}, {"3"c, 3}, {"4"c, 4}, {"5"c, 5}, {"6"c, 6}, {"7"c, 7}, {"8"c, 8}, {"9"c, 9},
{"A"c, 10}, {"B"c, 11}, {"C"c, 12}, {"D"c, 13}, {"E"c, 14}, {"F"c, 15},
{"a"c, 10}, {"b"c, 11}, {"c"c, 12}, {"d"c, 13}, {"e"c, 14}, {"f"c, 15}, ' Fullwidth versions are next.
{"0"c, 0}, {"1"c, 1}, {"2"c, 2}, {"3"c, 3}, {"4"c, 4},
{"5"c, 5}, {"6"c, 6}, {"7"c, 7}, {"8"c, 8}, {"9"c, 9},
{"A"c, 10}, {"B"c, 11}, {"C"c, 12}, {"D"c, 13}, {"E"c, 14}, {"F"c, 15},
{"a"c, 10}, {"b"c, 11}, {"c"c, 12}, {"d"c, 13}, {"e"c, 14}, {"f"c, 15}}
Private Shared _CharacterValues2_ As New Dictionary(Of Char, Byte)(capacity:=32) _
From {{"0"c, 0}, {"1"c, 1}, {"2"c, 2}, {"3"c, 3}, {"4"c, 4}, {"5"c, 5}, {"6"c, 6}, {"7"c, 7}, {"8"c, 8}, {"9"c, 9},
{"A"c, 10}, {"B"c, 11}, {"C"c, 12}, {"D"c, 13}, {"E"c, 14}, {"F"c, 15},
{"a"c, 10}, {"b"c, 11}, {"c"c, 12}, {"d"c, 13}, {"e"c, 14}, {"f"c, 15}} ' Fullwidth versions are next.
Private Shared _cv As New Dictionary(Of Char, Byte)(capacity:=32) From
{{"0"c, 0}, {"1"c, 1}, {"2"c, 2}, {"3"c, 3}, {"4"c, 4},
{"5"c, 5}, {"6"c, 6}, {"7"c, 7}, {"8"c, 8}, {"9"c, 9},
{"A"c, 10}, {"B"c, 11}, {"C"c, 12}, {"D"c, 13}, {"E"c, 14}, {"F"c, 15},
{"a"c, 10}, {"b"c, 11}, {"c"c, 12}, {"d"c, 13}, {"e"c, 14}, {"f"c, 15}}
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Friend Shared Function Method1(Digit As Char) As Byte
Dim value As Byte
If _CharacterValues_.TryGetValue(Digit, value) = False Then Return 0
Return value
End Function
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Friend Shared Function Method4(Digit As Char) As Byte
Dim value As Byte
If _CharacterValues2_.TryGetValue(Digit, value) = False Then
If _cv.TryGetValue(MakeHalfWidth(Digit), value) = False Then
Return 0
End If
End If
Return value
End Function
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Friend Shared Function Method5(Digit As Char) As Byte
Select Case Digit
Case "0"c : Return 0
Case "1"c : Return 1
Case "2"c : Return 2
Case "3"c : Return 3
Case "4"c : Return 4
Case "5"c : Return 5
Case "6"c : Return 6
Case "7"c : Return 7
Case "8"c : Return 8
Case "9"c : Return 9
Case "A"c : Return 10
Case "B"c : Return 11
Case "C"c : Return 12
Case "D"c : Return 13
Case "E"c : Return 14
Case "F"c : Return 15
Case "a"c : Return 10
Case "b"c : Return 11
Case "c"c : Return 12
Case "d"c : Return 13
Case "e"c : Return 14
Case "f"c : Return 15
' Fullwidth versions are next.
Case "0"c : Return 0
Case "1"c : Return 1
Case "2"c : Return 2
Case "3"c : Return 3
Case "4"c : Return 4
Case "5"c : Return 5
Case "6"c : Return 6
Case "7"c : Return 7
Case "8"c : Return 8
Case "9"c : Return 9
Case "A"c : Return 10
Case "B"c : Return 11
Case "C"c : Return 12
Case "D"c : Return 13
Case "E"c : Return 14
Case "F"c : Return 15
Case "a"c : Return 10
Case "b"c : Return 11
Case "c"c : Return 12
Case "d"c : Return 13
Case "e"c : Return 14
Case "f"c : Return 15
Case Else
Return 0
End Select
End Function
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Friend Shared Function Method6(Digit As Char) As Byte
Select Case Digit
Case "0"c : Return 0
Case "1"c : Return 1
Case "2"c : Return 2
Case "3"c : Return 3
Case "4"c : Return 4
Case "5"c : Return 5
Case "6"c : Return 6
Case "7"c : Return 7
Case "8"c : Return 8
Case "9"c : Return 9
Case "A"c : Return 10
Case "B"c : Return 11
Case "C"c : Return 12
Case "D"c : Return 13
Case "E"c : Return 14
Case "F"c : Return 15
Case "a"c : Return 10
Case "b"c : Return 11
Case "c"c : Return 12
Case "d"c : Return 13
Case "e"c : Return 14
Case "f"c : Return 15
Case Else
Select Case Digit ' Fullwidth versions are next.
Case "0"c : Return 0
Case "1"c : Return 1
Case "2"c : Return 2
Case "3"c : Return 3
Case "4"c : Return 4
Case "5"c : Return 5
Case "6"c : Return 6
Case "7"c : Return 7
Case "8"c : Return 8
Case "9"c : Return 9
Case "A"c : Return 10
Case "B"c : Return 11
Case "C"c : Return 12
Case "D"c : Return 13
Case "E"c : Return 14
Case "F"c : Return 15
Case "a"c : Return 10
Case "b"c : Return 11
Case "c"c : Return 12
Case "d"c : Return 13
Case "e"c : Return 14
Case "f"c : Return 15
Case Else
Return 0
End Select
End Select
End Function
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Friend Shared Function Method3(Digit As Char) As Byte
Dim value As Byte
If _CharacterValues2_.TryGetValue(Digit, value) = False Then
If _CharacterValues2_.TryGetValue(MakeHalfWidth(Digit), value) = False Then
Return 0
End If
End If
Return value
End Function
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Friend Shared Function Method2(Digit As Char) As Byte
Dim xi = Array.BinarySearch(Of Char)(_Keys, Digit)
If xi < 0 Then
' Debug.Assert(False, "Surprising digit.")
Return 0
End If
Return _Values(xi)
End Function
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Friend Shared Function IntegralLiteralCharacterValue(Digit As Char) As Integer
Dim value As Integer = AscW(Digit) - AscW("0"c)
If (CType(value, UInt32) > CType(9, UInt32)) Then
value = IntegralLiteralCharacterValueUncommon(Digit)
End If
Return value
End Function
Private Shared Function IntegralLiteralCharacterValueUncommon(Digit As Char) As Integer
Dim u As Integer = AscW(Digit)
If Digit >= "A"c And Digit <= "F"c Then
Return (u + (10 - AscW("A"c)))
End If
If Digit >= "a"c And Digit <= "f"c Then
Return (u + (10 - AscW("a"c)))
End If
'Debug.Assert(IsFullWidth(Digit), "Surprising digit")
Return IntegralLiteralCharacterValue(MakeHalfWidth(Digit))
End Function
'// MakeHalfWidth - Converts a full-width character to half-width
Friend Shared Function MakeHalfWidth(c As Char) As Char
Debug.Assert(IsFullWidth(c))
Return Convert.ToChar(Convert.ToUInt16(c) - s_fullwidth)
End Function
'// IsFullWidth - Returns if the character is full width
Friend Shared Function IsFullWidth(c As Char) As Boolean
' Do not use "AndAlso" or it will not inline.
Return c > ChrW(&HFF00US) And c < ChrW(&HFF5FUS)
End Function
Private Const s_fullwidth = CInt(&HFF00L - &H0020L)
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Friend Shared Function IsBetween(value As Char, min As Char, max As Char) As Boolean
Return (min <= value And value <= max)
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment