程序加速之查表法
查表法是通常采用的一种以空间(内存空间)换时间(运行时间)的程序加速方法
,基本过程是把需要经常查询数据库或复杂浮点计算的结果保存到一个数据结构中
(数组最为常见),以后只要根据条件取出相应的数据即可。比如把1-360度的正
切值保存到数组中,当输入45度时,只需取出第45个元素。这样就节约了不必要的
计算时间。
对于作为基本定义使用的表,数据不经常改变的数据表也可以使用这种方法。
例:有一个表MB80K020,有SCOM,FUKACODE,NMOJI,NMOJISU四个列,分别代表行号
,列号,可以输入的文字,可以输入的文字长度。对此我们可以如此操作,
1.在进入画面时将所有情况查询出来放到数组中。
arrayAllChars = get2dArrayBySql(" select SCOM,FUKACODE,NMOJI,NMOJISU from MB80K020 ")
2.需要用到的时候取出数组中的数据即可.下面是两个取数的函数.
Private Function getAllowChars(scom As String, fukacode As String) As String
Dim i As Integer
Dim iMax As Integer
getAllowChars = ""
iMax = UBound(arrayAllChars, 1)
For i = 0 To iMax
If scom = arrayAllChars(i, 0) And fukacode = arrayAllChars(i, 1) Then
getAllowChars = arrayAllChars(i, 2)
Exit Function
End If
Next i
End Function
Private Function getNmojiShu(scom As String, fukacode As String) As Integer
Dim i As Integer
Dim iMax As Integer
getNmojiShu = 0
iMax = UBound(arrayAllChars, 1)
For i = 0 To iMax
If scom = arrayAllChars(i, 0) And fukacode = arrayAllChars(i, 1) Then
getNmojiShu = CInt(Val(arrayAllChars(i, 3)))
Exit Function
End If
Next i
End Function
注意:
1.表的数据常发生变化不能使用此方法,否则会造成错误.
2.对于结果数组,适当采用排序和二分查找更能提高查询速度.
