j0kky Posted October 12, 2014 Share Posted October 12, 2014 Hi guys! In old topics there are a lot of functions that convert a binary (Base2) number in a decimal (base10) number. I thought it would be funny if we tried the "best" function that do it: for example I wrote some functions and this is the fastest one. Func _BinToDec($bin) Local $dec = 0, $array = StringSplit($bin, "") For $i = $array[0] To 1 Step -1 If $array[$i] = "1" Then $dec += 2 ^ ($array[0] - $i) Next Return $dec EndFunc I tested it in this way: Global $num = 1001010101 $t = TimerInit() While TimerDiff($t) < 30000 StringLen($num) WEnd $t = TimerInit() For $i = 1 To 100000 BinToDec($num) Next ConsoleWrite(TimerDiff($t) / 100000 & @CRLF & BinToDec($num) & @CRLF & @CRLF) The first 30-second-part is just to heat the processor. Do you think you can do it better? Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
Gianni Posted October 13, 2014 Share Posted October 13, 2014 .... this is a bit faster (and there is no need to heat the processor) ... Func _BinToDec($num) Local $dec = 0, $len = StringLen($num) For $i = 0 To $len - 1 $dec += 2 ^ $i * StringMid($num, $len - $i, 1) Next Return $dec EndFunc ;==>_BinToDec Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
kaesereibe Posted October 14, 2014 Share Posted October 14, 2014 (edited) Another way =) (dont know if faster or not) Func _BinToDec($B) Return BitOr((StringLen($B) > 1 ? BitShift(_BinToDec(StringTrimRight($B, 1)), -1) : 0), StringRight($B, 1)) EndFunc ;==> _BinToDec() AutoIt v3.3.12.0 Edited October 14, 2014 by kaesereibe BIN 2 DEC | ConvertTemp | DEC 2 BIN | GetWeekday | HEX 2 RGB | INT 2 HEX | QueryPerformance Link to comment Share on other sites More sharing options...
kaesereibe Posted October 14, 2014 Share Posted October 14, 2014 And another one (Can be improved more) Func _BinToDec($sNumber) Local Static $tChr = DllStructCreate("char[64];") Local $iNum, $aRes $aRes = DllCall("msvcrt.dll", "uint64:cdecl", "_strtoui64", "str", $sNumber, "ptr", 0, "int", 2) $iNum = $aRes[0] $aRes = DllCall("msvcrt.dll", "ptr:cdecl", "_i64toa", "int64", $iNum, "ptr", DllStructGetPtr($tChr), "int", 10) Return DllStructGetData($tChr, 1) EndFunc BIN 2 DEC | ConvertTemp | DEC 2 BIN | GetWeekday | HEX 2 RGB | INT 2 HEX | QueryPerformance Link to comment Share on other sites More sharing options...
j0kky Posted October 16, 2014 Author Share Posted October 16, 2014 Hi guys I made lot of tests but I really can't figure out which of your functions is faster... Congratulations to all Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
Gianni Posted October 16, 2014 Share Posted October 16, 2014 Hi guys I made lot of tests but I really can't figure out which of your functions is faster... Congratulations to all it seems that: kaesereibe's first attempt in post #3 (recursive) is the slowest and also it fails with long binary numbers, while time taken from kaesereibe's way from post #4 is very fast and is not affected from the length of the binary number expandcollapse popupGlobal $aRacers[4] = ["j0kky", "Chimp", "kaesereibe_1", "kaesereibe_2"] Go("1001010101") ConsoleWrite("---------------" & @CRLF) Go("100101010110101") ConsoleWrite("---------------" & @CRLF) Go("10010101011001010101") ConsoleWrite("---------------" & @CRLF) Go("1001010101100101010110010101011001010101") Func Go($num) For $Racer = 0 To UBound($aRacers) - 1 $t = TimerInit() For $i = 1 To 100000 Call($aRacers[$Racer], $num) Next ConsoleWrite(StringFormat("%-40s", $aRacers[$Racer] & "'s performance is ") & TimerDiff($t) / 1000 & " sec." & @TAB & "converted value is " & Call($aRacers[$Racer], $num) & @CRLF) Next EndFunc ;==>Go Func j0kky($num) Local $dec = 0, $array = StringSplit($num, "") For $i = $array[0] To 1 Step -1 If $array[$i] = "1" Then $dec += 2 ^ ($array[0] - $i) Next Return $dec EndFunc ;==>j0kky Func Chimp($num) Local $dec = 0, $len = StringLen($num) For $i = 0 To $len - 1 $dec += 2 ^ $i * StringMid($num, $len - $i, 1) Next Return $dec EndFunc ;==>Chimp Func kaesereibe_1($num) Return BitOR((StringLen($num) > 1 ? BitShift(kaesereibe_1(StringTrimRight($num, 1)), -1) : 0), StringRight($num, 1)) EndFunc ;==>kaesereibe_1 Func kaesereibe_2($num) Local Static $tChr = DllStructCreate("char[64];") Local $iNum, $aRes $aRes = DllCall("msvcrt.dll", "uint64:cdecl", "_strtoui64", "str", $num, "ptr", 0, "int", 2) $iNum = $aRes[0] $aRes = DllCall("msvcrt.dll", "ptr:cdecl", "_i64toa", "int64", $iNum, "ptr", DllStructGetPtr($tChr), "int", 10) Return DllStructGetData($tChr, 1) EndFunc ;==>kaesereibe_2 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
j0kky Posted October 17, 2014 Author Share Posted October 17, 2014 (edited) while time taken from kaesereibe's way from post #4 is very fast and is not affected from the length of the binary number I see... probably it is because the execution of _strtoui64 and_i64toa requires an infinitesimal time and the total time of kaesereibe's function is composed only by the starting time of those APIs. Edited October 17, 2014 by j0kky Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now