Jump to content

Binary as InputBox to decimal and hex?


Recommended Posts

28 minutes ago, major_lee said:

I think its only fair to extend the code to handle more then the limited bytes as originally assumed.

Note : Your modified version still contains the typo :

Local $sBasedOn="JockoDundee with major_lee edit", $iIter=1000000, $sOut, $t=TimerInit()
[...]
$arr[1011]="E"

It must be : $arr[1110] ="E" 

17 minutes ago, JockoDundee said:

Anybody have any theories why 64bit vs 32bit would make such a big difference?

Is the time difference reproducible on your PC if you set #AutoIt3Wrapper_UseX64 = N at the beginning of both scripts ? Is Chimp's script faster than yours at 32-bit then ?

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

5 minutes ago, Musashi said:

Is the time difference reproducible on your PC if you set #AutoIt3Wrapper_UseX64 = N at the beginning of both scripts ? Is Chimp's script faster than yours at 32-bit then ?

 

 

I would consider it a significant difference for my system.

Quote

Based on code from: JockoDundee with major_lee edit
 Check: AA 170 
 1000000 Iterations in 11.2600858 Seconds
Based on code from: JockoDundee with major_lee edit
 Check: AA 170 
 1000000 Iterations in 11.0764397 Seconds
Based on code from: JockoDundee with major_lee edit
 Check: AA 170 
 1000000 Iterations in 11.5386483 Seconds

#AutoIt3Wrapper_UseX64 = N
Local $sBasedOn="JockoDundee with major_lee edit", $iIter=1000000, $sOut, $t=TimerInit()

Local $dataStream="10101010", $arr[1112]

$arr[0]="0"
$arr[1]="1"
$arr[10]="2"
$arr[11]="3"
$arr[100]="4"
$arr[101]="5"
$arr[110]="6"
$arr[111]="7"
$arr[1000]="8"
$arr[1001]="9"
$arr[1010]="A"
$arr[1011]="B"
$arr[1100]="C"
$arr[1101]="D"
$arr[1110] ="E"
$arr[1111]="F"


For $n=1 To $iIter
    $sbyte = $dataStream
    $sOut = Null
    Local $bArray[StringLen ( $sbyte )/4+1]
    $bArray[0] = (StringLen ( $sbyte )/4)

    for $z = 1 To $bArray[0]
        $bArray[$z] = $arr[StringLeft($sbyte, 4)]
        $sbyte = StringTrimLeft ( $sbyte, 4 )
        $sOut=$sOut&$bArray[$z]
    Next
    $sOut=$sOut &" "& Dec($sOut)
Next

$t=TimerDiff($t)

ConsoleWrite("Based on code from: "& $sBasedOn & @CRLF& " Check: "& $sOut &" "& @CRLF &" "& $iIter &" Iterations in "& $t/1000  &" Seconds" &@CRLF&@CRLF)

 

Quote

Based on code from: JockoDundee with major_lee edit
 Check: AA 170 
 1000000 Iterations in 10.0264897 Seconds
Based on code from: JockoDundee with major_lee edit
 Check: AA 170 
 1000000 Iterations in 10.2769168 Seconds
Based on code from: JockoDundee with major_lee edit
 Check: AA 170 
 1000000 Iterations in 9.7342313 Seconds

 

#AutoIt3Wrapper_UseX64 = Y
Local $sBasedOn="JockoDundee with major_lee edit", $iIter=1000000, $sOut, $t=TimerInit()

Local $dataStream="10101010", $arr[1112]

$arr[0]="0"
$arr[1]="1"
$arr[10]="2"
$arr[11]="3"
$arr[100]="4"
$arr[101]="5"
$arr[110]="6"
$arr[111]="7"
$arr[1000]="8"
$arr[1001]="9"
$arr[1010]="A"
$arr[1011]="B"
$arr[1100]="C"
$arr[1101]="D"
$arr[1110] ="E"
$arr[1111]="F"


For $n=1 To $iIter
    $sbyte = $dataStream
    $sOut = Null
    Local $bArray[StringLen ( $sbyte )/4+1]
    $bArray[0] = (StringLen ( $sbyte )/4)

    for $z = 1 To $bArray[0]
        $bArray[$z] = $arr[StringLeft($sbyte, 4)]
        $sbyte = StringTrimLeft ( $sbyte, 4 )
        $sOut=$sOut&$bArray[$z]
    Next
    $sOut=$sOut &" "& Dec($sOut)
Next

$t=TimerDiff($t)

ConsoleWrite("Based on code from: "& $sBasedOn & @CRLF& " Check: "& $sOut &" "& @CRLF &" "& $iIter &" Iterations in "& $t/1000  &" Seconds" &@CRLF&@CRLF)

 

 

Quote

Note : Your modified version still contains the typo :

Yes I figured I would forget to address that. lol

Link to comment
Share on other sites

16 hours ago, Musashi said:

Is Chimp's script faster than yours at 32-bit then ?

32-bit:

Based on code from: Chimp
 Check: AA 170
 1000000 Iterations in 2.9068673 Seconds

Based on code from: JockoDundee
 Check: AA 170
 1000000 Iterations in 2.5073343 Seconds

64-bit:

Based on code from: Chimp
 Check: AA 170
 1000000 Iterations in 2.7761728 Seconds

Based on code from: JockoDundee
 Check: AA 170
 1000000 Iterations in 2.2305445 Seconds

 

Code hard, but don’t hard code...

Link to comment
Share on other sites

Ahem, I'm wondering what can be the use case of a base conversion routine run a few hundred of thousands times a second.  In which context can speed be a first-order discriminant for such a piece of code?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

59 minutes ago, jchd said:

I'm wondering what can be the use case of a base conversion routine run a few hundred of thousands times a second.  In which context can speed be a first-order discriminant for such a piece of code?

I had mentioned this issue as well :).

On 5/10/2021 at 6:50 PM, Musashi said:

100000 iterations were intended as a 'hardcore' scenario. You will probably need only a few conversions, especially when using InputBox. The conversion time is therefore negligible.

The whole topic has now evolved into a rather theoretical question, i.e. : "Why does compiling with x64 or x86 on different systems lead to the stated runtime behavior."

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

I've seen that.

I've given up trying to sort out why in some cases a X86 script is faster than X64 or vice-versa. There are way too many parameters involved and unless I feel speed a must-have, I don't care anymore.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

5 hours ago, jchd said:

Ahem, I'm wondering what can be the use case of a base conversion routine run a few hundred of thousands times a second.  In which context can speed be a first-order discriminant for such a piece of code?

Many things will be impacting it.
Baseline makes a big difference. Is the environment the same being ran?
My baseline was just closing down every other process so they could be ran independently. GOod enough for me.
I ran a few test with the internet open and there was 1-2 second difference in the numbers.

Is it being compiled or ran through SciTE?
How is the program/code being prioritized with the cpu? 
Version of windows?

To really understand it would need to be broken down entirely.
How the compiler, assembly and processor. Each thing could be impacting it significantly, the software, hardware, firmware and variation of versions.
I dont think I'm really qualified to say, if I did say, everything(programs,software,hardware) else impacting it without a proper baseline. is the simple answer.

Until that point is reached, then the baselines are not accurate enough to really determine which is faster.
I think a easy way to do a proper baseline might be run windows in safe mode? Is that still possible? never tried in this version of windows.. lol

Link to comment
Share on other sites

5 hours ago, jchd said:

Ahem, I'm wondering what can be the use case of a base conversion routine run a few hundred of thousands times a second.

I’m wondering as well.

On the other hand, there are use cases where even a single extra assignment or function call in a routine can add up to minutes or hours of added execution time.

Perhaps, you recall the curious case of the “Four Pillars of Destiny”:

Those type of CPU only programs are rare, but valid.

In a similar scenario, if one was faced with a conversion of huge text files containing only binary strings, it might make a difference if one algorithm was 10x faster.

Still, for myself I could see myself using your algorithm, in a normal scenario, just from a pragmatic point of view.

Code hard, but don’t hard code...

Link to comment
Share on other sites

Spoiler
Tester()
Func Tester()
    Local $t = TimerInit()
    For $n = 1 To 1000 ;>>> StrBin2Hex_Array  $sResult AA    $t: 9.4973
        $sResult = StrBin2Hex_Array("10101010")
    Next
    $t = TimerDiff($t)
    ConsoleWrite(">>> StrBin2Hex_Array   $sResult " & $sResult & @TAB & " $t: " & $t & " ms." & @CRLF)

    $t = TimerInit()
    For $n = 1 To 1000 ;>>> StrBin2Hex_Static $sResult AA    $t: 15.5145
        $sResult = StrBin2Hex_Static("10101010")
    Next
    $t = TimerDiff($t)
    ConsoleWrite(">>> StrBin2Hex_Static  $sResult " & $sResult & @TAB & " $t: " & $t & " ms." & @CRLF)

    $t = TimerInit()
    For $n = 1 To 1000 ;>>> StrBin2Hex_Enum   $sResult AA    $t: 14.0042
        $sResult = StrBin2Hex_Enum("10101010")
    Next
    $t = TimerDiff($t)
    ConsoleWrite(">>> StrBin2Hex_Enum    $sResult " & $sResult & @TAB & " $t: " & $t & " ms." & @CRLF)

    $t = TimerInit()
    For $n = 1 To 1000 ;>>> StrBin2Hex_Switch $sResult AA    $t: 11.2475
        $sResult = StrBin2Hex_Switch("10101010")
    Next
    $t = TimerDiff($t)
    ConsoleWrite(">>> StrBin2Hex_Switch  $sResult " & $sResult & @TAB & " $t: " & $t & " ms." & @CRLF)

    $t = TimerInit()
    For $n = 1 To 1000 ;>>> StrBin2Hex_Dict   $sResult AA    $t: 17.2715
        $sResult = StrBin2Hex_Dict("10101010")
    Next
    $t = TimerDiff($t)
    ConsoleWrite(">>> StrBin2Hex_Dict    $sResult " & $sResult & @TAB & " $t: " & $t & " ms." & @CRLF)
    $t = TimerInit()

    For $n = 1 To 1000 ;>>> StrBin2Hex_Struct $sResult AA    $t: 14.0702
        $sResult = StrBin2Hex_Struct("10101010")
    Next
    $t = TimerDiff($t)
    ConsoleWrite(">>> StrBin2Hex_Struct  $sResult " & $sResult & @TAB & " $t: " & $t & " ms." & @CRLF)

EndFunc   ;==>Tester
Exit

Func StrBin2Hex_Static($sBin) ; >>> $sResult AA  $t: 7.1451
    Local Static $0000 = 0, $0001 = 1, $0010 = 2, $0011 = 3, $0100 = 4, $0101 = 5, $0110 = 6, $0111 = 7, _
            $1000 = 8, $1001 = 9, $1010 = "A", $1011 = "B", $1100 = "C", $1101 = "D", $1110 = "E", $1111 = "F"
    Local $sResult = ""
    Do
        $sResult &= Eval(StringLeft($sBin, 4))
        $sBin = StringTrimLeft($sBin, 4)
    Until $sBin = ""
    Return $sResult
EndFunc   ;==>StrBin2Hex_Static

Func StrBin2Hex_Enum($sBin) ; >>> $sResult AA    $t: 7.1451
    Local Enum $0000 = 0, $0001 = 1, $0010 = 2, $0011 = 3, $0100 = 4, $0101 = 5, $0110 = 6, $0111 = 7, _
            $1000 = 8, $1001 = 9, $1010 = "A", $1011 = "B", $1100 = "C", $1101 = "D", $1110 = "E", $1111 = "F"
    Local $sResult = ""
    Do
        $sResult &= Eval(StringLeft($sBin, 4))
        $sBin = StringTrimLeft($sBin, 4)
    Until $sBin = ""
    Return $sResult
EndFunc   ;==>StrBin2Hex_Enum

Func StrBin2Hex_Array($sBin) ; >>> $sResult AA   $t: 6.61
    Local Static $arr[1112]
    If Not $arr[1] Then
        $arr[0] = "0"
        $arr[1] = "1"
        $arr[10] = "2"
        $arr[11] = "3"
        $arr[100] = "4"
        $arr[101] = "5"
        $arr[110] = "6"
        $arr[111] = "7"
        $arr[1000] = "8"
        $arr[1001] = "9"
        $arr[1010] = "A"
        $arr[1011] = "B"
        $arr[1100] = "C"
        $arr[1101] = "D"
        $arr[1110] = "E"
        $arr[1111] = "F"
    EndIf
    Local $sResult = ""
    Do
        $sResult &= $arr[StringLeft($sBin, 4)]
        $sBin = StringTrimLeft($sBin, 4)
    Until $sBin = ""
    Return $sResult
EndFunc   ;==>StrBin2Hex_Array


Func StrBin2Hex_Switch($sBin) ; >>> $sResult AA  $t: 11.7474
    Local $sResult = ""
    Do
        Switch StringLeft($sBin, 4)
            Case "0000"
                $sResult &= "0"
            Case "0001"
                $sResult &= "1"
            Case "0010"
                $sResult &= "2"
            Case "0011"
                $sResult &= "3"
            Case "0100"
                $sResult &= "4"
            Case "0101"
                $sResult &= "5"
            Case "0110"
                $sResult &= "6"
            Case "0111"
                $sResult &= "7"
            Case "1000"
                $sResult &= "8"
            Case "1001"
                $sResult &= "9"
            Case "1010"
                $sResult &= "A"
            Case "1011"
                $sResult &= "B"
            Case "1100"
                $sResult &= "C"
            Case "1101"
                $sResult &= "D"
            Case "1110"
                $sResult &= "E"
            Case "1111"
                $sResult &= "F"
        EndSwitch
        $sBin = StringTrimLeft($sBin, 4)
    Until $sBin = ""
    Return $sResult
EndFunc   ;==>StrBin2Hex_Switch

Func StrBin2Hex_Dict($sBin)
    Local Static $o = ObjCreate("Scripting.Dictionary")
    If Not $o.Exists("0001") Then
        $o.Add("0000", "0")
        $o.Add("0001", "1")
        $o.Add("0010", "2")
        $o.Add("0011", "3")
        $o.Add("0100", "4")
        $o.Add("0101", "5")
        $o.Add("0110", "6")
        $o.Add("0111", "7")
        $o.Add("1000", "8")
        $o.Add("1001", "9")
        $o.Add("1010", "A")
        $o.Add("1011", "B")
        $o.Add("1100", "C")
        $o.Add("1101", "D")
        $o.Add("1110", "E")
        $o.Add("1111", "F")
    EndIf
    Local $sResult = ""
    Do
        $sResult &= $o.Item(StringLeft($sBin, 4))
        $sBin = StringTrimLeft($sBin, 4)
    Until $sBin = ""
    Return $sResult
EndFunc   ;==>StrBin2Hex_Dict

Func StrBin2Hex_Struct($sBin)
    Local Static $iInit = 1, $tagSBINTHING = "struct;CHAR 0000;CHAR 0001;CHAR 0010;CHAR 0011;CHAR 0100;CHAR 0101;CHAR 0110;CHAR 0111;" & _
            "CHAR 1000;CHAR 1001;CHAR 1010;CHAR 1011;CHAR 1100;CHAR 1101;CHAR 1110;CHAR 1111;endstruct"
    Local Static $tSBINTHING = DllStructCreate($tagSBINTHING) ; ...GETTING FANCY HERE   ;)
    If $iInit Then
        $iInit = 0
        DllStructSetData($tSBINTHING, "0000", "0")
        DllStructSetData($tSBINTHING, "0001", "1")
        DllStructSetData($tSBINTHING, "0010", "2")
        DllStructSetData($tSBINTHING, "0011", "3")
        DllStructSetData($tSBINTHING, "0100", "4")
        DllStructSetData($tSBINTHING, "0101", "5")
        DllStructSetData($tSBINTHING, "0110", "6")
        DllStructSetData($tSBINTHING, "0111", "7")
        DllStructSetData($tSBINTHING, "1000", "8")
        DllStructSetData($tSBINTHING, "1001", "9")
        DllStructSetData($tSBINTHING, "1010", "A")
        DllStructSetData($tSBINTHING, "1011", "B")
        DllStructSetData($tSBINTHING, "1100", "C")
        DllStructSetData($tSBINTHING, "1101", "D")
        DllStructSetData($tSBINTHING, "1110", "E")
        DllStructSetData($tSBINTHING, "1111", "F")
    EndIf
    Local $sResult = ""
    Do
        $sResult &= DllStructGetData($tSBINTHING, StringLeft($sBin, 4))
        $sBin = StringTrimLeft($sBin, 4)
    Until $sBin = ""
    Return $sResult
EndFunc   ;==>StrBin2Hex_Struct

 

This should have been wrapped in a function and run as such. Otherwise the results are not real life results. Resulting in misconceptions on expectation of functionality.
Nevertheless, all these are sub millisecond run. So much so that I have to run'em 1000 times to get a steady above millisecond measurement.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...