Sign in to follow this  
Followers 0
j0kky

Binary to Decimal game

7 posts in this topic

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?  :P

Share this post


Link to post
Share on other sites



.... 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

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Hi guys

I made lot of tests but I really can't figure out which of your functions is faster... Congratulations to all  :thumbsup:  :thumbsup:  :thumbsup:

 

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

Global $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

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#7 ·  Posted (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 by j0kky

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0