Office中国论坛/Access中国论坛
标题:
HackVBASpeed 之进制转换,之二进制转十进制.极限挑战
[打印本页]
作者:
wang1999
时间:
2015-8-9 11:10
标题:
HackVBASpeed 之进制转换,之二进制转十进制.极限挑战
标题有点浮夸了, 各位看官随便拍
{:soso_e112:},
有好的代码尽管来挑战
{:soso_e129:}
进制转换基本在VBA常常遇到, 也是最基础的函数, 之前写的转换今天看来如果进行深度优化, 速度提升可能有100倍.
0. 不限语言(汇编,C,VB,API, TLB都可以),
1. 能在VBA环境调用,
2. 能完成1~32位内长度的二进制字符串转换
作者:
wang1999
时间:
2015-8-9 11:19
这个我目前收集到VBA速度最快的二进制转十进制, 德国人写的, 但可惜只能定长字符串(32字符长度)
当然这是我收集,不是我写的, 如果要重写定长转换, 速度还是很容易超过它的. 但定长的实用性不强, 写出后意义也不大.
Public Function BitToLong04(bitexpr As String) As Long
' by G.Beckmann,
G.Beckmann@NikoCity.de
, 20001230
' based on BitToLong03 by Egbert Nierop,
egbert_nierop@goovy.hotmail.com
, 20001228
Static t%(31): Dim asc0%
If Len(bitexpr) <> 32 Then Exit Function
RtlMoveMemory t(0), ByVal StrPtr(bitexpr), 64
asc0 = KeyCodeConstants.vbKey0
BitToLong04 = t(1) - asc0
BitToLong04 = 2 * BitToLong04 + t(2) - asc0
BitToLong04 = 2 * BitToLong04 + t(3) - asc0
BitToLong04 = 2 * BitToLong04 + t(4) - asc0
BitToLong04 = 2 * BitToLong04 + t(5) - asc0
BitToLong04 = 2 * BitToLong04 + t(6) - asc0
BitToLong04 = 2 * BitToLong04 + t(7) - asc0
BitToLong04 = 2 * BitToLong04 + t(8) - asc0
BitToLong04 = 2 * BitToLong04 + t(9) - asc0
BitToLong04 = 2 * BitToLong04 + t(10) - asc0
BitToLong04 = 2 * BitToLong04 + t(11) - asc0
BitToLong04 = 2 * BitToLong04 + t(12) - asc0
BitToLong04 = 2 * BitToLong04 + t(13) - asc0
BitToLong04 = 2 * BitToLong04 + t(14) - asc0
BitToLong04 = 2 * BitToLong04 + t(15) - asc0
BitToLong04 = 2 * BitToLong04 + t(16) - asc0
BitToLong04 = 2 * BitToLong04 + t(17) - asc0
BitToLong04 = 2 * BitToLong04 + t(18) - asc0
BitToLong04 = 2 * BitToLong04 + t(19) - asc0
BitToLong04 = 2 * BitToLong04 + t(20) - asc0
BitToLong04 = 2 * BitToLong04 + t(21) - asc0
BitToLong04 = 2 * BitToLong04 + t(22) - asc0
BitToLong04 = 2 * BitToLong04 + t(23) - asc0
BitToLong04 = 2 * BitToLong04 + t(24) - asc0
BitToLong04 = 2 * BitToLong04 + t(25) - asc0
BitToLong04 = 2 * BitToLong04 + t(26) - asc0
BitToLong04 = 2 * BitToLong04 + t(27) - asc0
BitToLong04 = 2 * BitToLong04 + t(28) - asc0
BitToLong04 = 2 * BitToLong04 + t(29) - asc0
BitToLong04 = 2 * BitToLong04 + t(30) - asc0
BitToLong04 = t(31) - asc0 + 2 * BitToLong04
If t(0) <> asc0 Then BitToLong04 = BitToLong04 Or &H80000000
End Function
作者:
admin
时间:
2015-8-10 08:59
赞一个!
作者:
wang1999
时间:
2015-8-10 12:49
admin 发表于 2015-8-10 08:59
赞一个!
这不是我的代码,=会上我的代码,再给我点个赞
.我的意思是挑战他的速度.
作者:
风中漫步
时间:
2015-8-10 16:47
静等大神们过招
作者:
wang1999
时间:
2015-8-10 17:42
风中漫步 发表于 2015-8-10 16:47
静等大神们过招
我是业余玩玩, 凑个热闹{:soso_e121:}
搞了半天, 由于那个德国人已经进行多版 的优化. 超过他不容易
'二进制 转 十进制.
'1.仅作对比使用.
'2.仅32位定长的二进制字符串.
Function BinToDecV6(BinString As String) As Long
If LenB(BinString) <> 64& Then Exit Function
Static saiShare() As Integer
Static sudtShare As UDT_SafeArray1D
'//1.初始化
With sudtShare
If .cDims Then '为了速度, 不采用 【If .cDims = 0 Then】, 因为条件判定式为 boolean = short int.
Else
'初始化数组
.cDims = 1
.cbElements = 2
.fFeatures = FADF_AUTO Or FADF_FIXEDSIZE
.clocks = 1 '防止 VB 在卸载或中止时销毁, 因为描述符为手动栈区分配
.cElements = 32 '元素数量
PtrArr(saiShare) = VarPtr(sudtShare) '数组变量赋值(描述符的首地址)
'初始化数据表
Static salDate1(0 To 31) As Long
Dim i As Long
For i = 0 To 31
salDate1(i) = ShL(1, i)
Next
End If
.pvData = StrPtr(BinString) '联接字符串数据
'//2.转换数据
If saiShare(0) And &H1 Then BinToDecV6 = salDate1(31)
If saiShare(1) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(30)
If saiShare(2) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(29)
If saiShare(3) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(28)
If saiShare(4) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(27)
If saiShare(5) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(26)
If saiShare(6) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(25)
If saiShare(7) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(24)
If saiShare(8) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(23)
If saiShare(9) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(22)
If saiShare(10) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(21)
If saiShare(11) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(20)
If saiShare(12) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(19)
If saiShare(13) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(18)
If saiShare(14) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(17)
If saiShare(15) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(16)
If saiShare(16) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(15)
If saiShare(17) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(14)
If saiShare(18) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(13)
If saiShare(19) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(12)
If saiShare(20) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(11)
If saiShare(21) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(10)
If saiShare(22) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(9)
If saiShare(23) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(8)
If saiShare(24) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(7)
If saiShare(25) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(6)
If saiShare(26) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(5)
If saiShare(27) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(4)
If saiShare(28) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(3)
If saiShare(29) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(2)
If saiShare(30) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(1)
If saiShare(31) And &H1 Then BinToDecV6 = BinToDecV6 Or salDate1(0)
'//3.清理工作
.pvData = 0& '断开联接
End With
End Function
复制代码
速度对比
比德国人的快了14.23%
[attach]57047[/attach]
作者:
wang1999
时间:
2015-8-10 20:55
又简化了一个
速度更快, 这个应该是VB下的极限版本了. 即使用C写估计也就这样了
Function BinToDecV62(BinString As String) As Long
Static saiShare() As Integer, sudtShare As UDT_SafeArray1D
'//1.初始化
If LenB(BinString) <> 64& Then Exit Function
With sudtShare
If .cDims Then '为了速度, 不采用 【If .cDims = 0 Then】, 因为条件判定式为 boolean = short int.
Else
'初始化数组
.cDims = 1
.cbElements = 2
.fFeatures = FADF_AUTO Or FADF_FIXEDSIZE
.clocks = 1 '防止 VB 在卸载或中止时销毁, 因为描述符为手动栈区分配
.cElements = 32 '元素数量
PtrArr(saiShare) = VarPtr(sudtShare) '数组变量赋值(描述符的首地址)
End If
.pvData = StrPtr(BinString) '联结字符串数据
'//2.转换数据
If saiShare(0) And &H1 Then BinToDecV62 = &H80000000
If saiShare(1) And &H1 Then BinToDecV62 = BinToDecV62 Or &H40000000
If saiShare(2) And &H1 Then BinToDecV62 = BinToDecV62 Or &H20000000
If saiShare(3) And &H1 Then BinToDecV62 = BinToDecV62 Or &H10000000
If saiShare(4) And &H1 Then BinToDecV62 = BinToDecV62 Or &H8000000
If saiShare(5) And &H1 Then BinToDecV62 = BinToDecV62 Or &H4000000
If saiShare(6) And &H1 Then BinToDecV62 = BinToDecV62 Or &H2000000
If saiShare(7) And &H1 Then BinToDecV62 = BinToDecV62 Or &H1000000
If saiShare(8) And &H1 Then BinToDecV62 = BinToDecV62 Or &H800000
If saiShare(9) And &H1 Then BinToDecV62 = BinToDecV62 Or &H400000
If saiShare(10) And &H1 Then BinToDecV62 = BinToDecV62 Or &H200000
If saiShare(11) And &H1 Then BinToDecV62 = BinToDecV62 Or &H100000
If saiShare(12) And &H1 Then BinToDecV62 = BinToDecV62 Or &H80000
If saiShare(13) And &H1 Then BinToDecV62 = BinToDecV62 Or &H40000
If saiShare(14) And &H1 Then BinToDecV62 = BinToDecV62 Or &H20000
If saiShare(15) And &H1 Then BinToDecV62 = BinToDecV62 Or &H10000
If saiShare(16) And &H1 Then BinToDecV62 = BinToDecV62 Or &H8000&
If saiShare(17) And &H1 Then BinToDecV62 = BinToDecV62 Or &H4000&
If saiShare(18) And &H1 Then BinToDecV62 = BinToDecV62 Or &H2000&
If saiShare(19) And &H1 Then BinToDecV62 = BinToDecV62 Or &H1000&
If saiShare(20) And &H1 Then BinToDecV62 = BinToDecV62 Or &H800&
If saiShare(21) And &H1 Then BinToDecV62 = BinToDecV62 Or &H400&
If saiShare(22) And &H1 Then BinToDecV62 = BinToDecV62 Or &H200&
If saiShare(23) And &H1 Then BinToDecV62 = BinToDecV62 Or &H100&
If saiShare(24) And &H1 Then BinToDecV62 = BinToDecV62 Or &H80&
If saiShare(25) And &H1 Then BinToDecV62 = BinToDecV62 Or &H40&
If saiShare(26) And &H1 Then BinToDecV62 = BinToDecV62 Or &H20&
If saiShare(27) And &H1 Then BinToDecV62 = BinToDecV62 Or &H10&
If saiShare(28) And &H1 Then BinToDecV62 = BinToDecV62 Or &H8&
If saiShare(29) And &H1 Then BinToDecV62 = BinToDecV62 Or &H4&
If saiShare(30) And &H1 Then BinToDecV62 = BinToDecV62 Or &H2&
If saiShare(31) And &H1 Then BinToDecV62 = BinToDecV62 Or &H1&
'//3.清理工作
.pvData = 0& '断开联结
End With
End Function
复制代码
测试结果如下, 与2楼的
BitToLong04
相比,
速度竟然提升了33.87%.
[attach]57053[/attach]
PS:把自己以前写的二进制转换十进制代码 作了一下对比. 速度是之前的25倍之多
欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/)
Powered by Discuz! X3.3