- 积分
- 73549
- 明经币
- 个
- 注册时间
- 2001-6-7
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2005-9-16 06:48:00
|
显示全部楼层
- Sub Main()
- Dim Str As String
- Dim tmpStr As String
- Dim tt As Single
-
- Str = GetStr(True)
- tt = Timer()
- tmpStr = ChsToCht0(Str)
- Debug.Print "使用逐个查找法的时间:" & Format(Timer() - tt, "0.00000") & "秒"
- tt = Timer()
- tmpStr = ChsToCht1(Str)
- Debug.Print "使用列表法的时间:" & Format(Timer() - tt, "0.00000") & "秒"
-
- tt = Timer()
- tmpStr = ChsToCht2(Str)
- Debug.Print "使用位置法的时间:" & Format(Timer() - tt, "0.00000") & "秒"
-
- End Sub
- Function ChsToCht1(Str As String) As String
- Dim i As Long
- Dim tmpStr As String
- Dim s As String
- Dim StrList As Variant
- StrList = GetStrList
- For i = 1 To Len(Str)
- s = Mid(Str, i, 1)
- If Asc(s) <= -2050 Then
- s = Chr(StrList(Asc(s) + 24159))
- End If
- tmpStr = tmpStr & s
- Next
- ChsToCht1 = tmpStr
- End Function
- Function ChsToCht2(Str As String) As String
- Dim ChsStr As String
- Dim ChtStr As String
- ChsStr = GetStr(True)
- ChtStr = GetStr(False)
- Dim i As Long
- Dim tmpStr As String
- Dim s As String
- Dim leftStr As String
- For i = 1 To Len(Str)
- s = Mid(Str, i, 1)
- If Asc(s) <= -2050 Then
- s = Mid(ChtStr, InStrRev(ChsStr, s), 1)
- End If
- tmpStr = tmpStr & s
- Next
- ChsToCht2 = tmpStr
- End Function
- Function ChsToCht0(Str As String) As String
- Dim ChsStr As String
- Dim ChtStr As String
- ChsStr = GetStr(True)
- ChtStr = GetStr(False)
- Dim i As Long
- Dim j As Long
- Dim tmpStr As String
- Dim s As String
- For i = 1 To Len(Str)
- s = Mid(Str, i, 1)
- For j = 1 To Len(ChsStr)
- If s = Mid(ChsStr, j, 1) Then
- s = Mid(ChtStr, j, 1)
- Exit For
- End If
- Next
- tmpStr = tmpStr & s
- Next
- ChsToCht0 = tmpStr
- End Function
- Function GetStrList() As Variant
- Dim StrList(22109) As Long
- Dim i As Long
- Dim ChsStr As String
- Dim ChtStr As String
- ChsStr = GetStr(True)
- ChtStr = GetStr(False)
- For i = 1 To Len(ChsStr)
- StrList(Asc(Mid(ChsStr, i, 1)) + 24159) = Asc(Mid(ChtStr, i, 1))
- Next
- GetStrList = StrList
- End Function
- Function GetStr(isChs As Boolean) As String
- Dim strFile As String
- Dim strPath As String
- Dim strText As String
- strFile = VBE.ActiveVBProject.FileName
- strPath = Left(strFile, InStrRev(strFile, ""))
- If isChs Then
- strFile = "chsstr.dat"
- Else
- strFile = "chtstr.dat"
- End If
- Open strPath & strFile For Input As #1
- Do Until EOF(1)
- Line Input #1, strText
- GetStr = GetStr & strText
- Loop
- Close #1
- End Function
运行后的速度比较:- 使用逐个查找法的时间:12.89063秒
- 使用列表法的时间:0.02930秒
- 使用位置法的时间:0.97070秒
复制代码 可以看到,我把整个简体表做为需要转换的字符串,整个运行速度中,列表法是最快的,而位置法则还不错。但查找法则不可行。
另外,查找法还可以进行优化,优化的条件是对照表中原始表(即简体表)字符按ASCII码排序,这样则可以通过筛选来一半一半筛选。如20个字,取第10个字,看ASC码与要比较字符的大小而决定是取前面一半的字符还是后面一半的字符继续进行比较。
|
|